如何动态地更改按钮的宽度 [英] how to change the width of a button dynamically

查看:95
本文介绍了如何动态地更改按钮的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建几个按钮的方法

I have a method that creates several buttons

public Button[] generaBottoniRisposta(int numeroBottoni, Context context){  
    Button bottoni[]= new Button[numeroBottoni];

    /*genero un tot di bottoni in base a numeroBottoni, è necessario avere il context*/
    for(int i=0; i < bottoni.length;i++){           
            bottoni[i] = new Button(context);
            bottoni[i].setId(i);
            bottoni[i].setText(String.valueOf(i+1));
            LayoutParams param = new LinearLayout.LayoutParams(50, 50);
            bottoni[i].setLayoutParams(param);          
        }

    return  bottoni;
    }

和那么它们添加到GridLayout的另一种方法。

and then another method that add them to a gridlayout.

我要设置这些按钮的宽度,但我不能够做到这一点。

I want to set the width of those buttons, but i'm not able to do it.

我试过了很多东西, setWidth() setMaxWidth()无效()

I tried a lot of stuff, setWidth(), setMaxWidth(), invalidate() etc.

奇怪的事情发生了。如果我试图做出比它的工作原理其默认大小的按钮更大,如果我尝试做出比其默认大小的按钮较小这是行不通的!

Something weird happens. If I try to make the button bigger than its default size it works, if i try to make the button smaller than its default size it doesn't work!

我应该怎么办?谢谢

推荐答案

我觉得setlayoutparams应该做你想要什么。像在该线程中的答案。
设置视图宽度编程

I think setlayoutparams should do what you want. Like in the answer in this thread. Set View Width Programmatically

这篇关于如何动态地更改按钮的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆