【问题标题】:Android : Unable to change width of dynamic buttonAndroid:无法更改动态按钮的宽度
【发布时间】:2015-08-23 19:08:40
【问题描述】:

我正在尝试制作固定尺寸的动态按钮。我可以改变高度但无法改变宽度。按钮的宽度好像是 MATCH_PARENT(占据整个屏幕宽度。如果有两个按钮,每个按钮宽度是屏幕宽度的一半,只有一个按钮时,按钮宽度是屏幕宽度)。

TableLayout table = (TableLayout)findViewById(r.id.table);
TableRow tableRow = new TableRow(this);
table.addView(tableRow);
Button button = new Button(this);
button.setLayoutParams(new TableRow.LayoutParams(30,30));
tableRow.addView(button);

谁能指出我哪里出错了。

【问题讨论】:

    标签: java android button dynamic layoutparams


    【解决方案1】:

    只有这个你必须做动态改变按钮

    首先我得到按钮

     Button btn = (Button) findViewById(R.id.button1);
    

    然后按钮点击事件我只放了这段代码

    TableRow.LayoutParams lp = new TableRow.LayoutParams(70, 70);
    btn.setLayoutParams(lp);
    

    并且工作正常。试试吧。会解决你的问题

    【讨论】:

      【解决方案2】:

      设置“button.setLayoutParams(new TableRow.LayoutParams(30,30));”更改为以下代码更改按钮的高度和宽度。

       LayoutParams lp = new LayoutParams(40, 40);
          table.addView(button, lp);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-22
        • 2019-12-21
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        • 2020-07-25
        • 2014-05-07
        • 2022-01-22
        相关资源
        最近更新 更多