7

我正在尝试通过代码动态添加按钮。

但我坚持如何通过代码设置android:layout_width按钮。0dp

4

2 回答 2

16

尝试这个 :

LayoutParams lp = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
myButton.setLayoutParams(lp);

进口,

import android.view.ViewGroup.LayoutParams;

谢谢

于 2013-01-25T19:39:26.510 回答
4

Your button have layoutParams. Get those params, update the width and reapply them:

ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)myButton.getLayoutParams();
lp.width = 0;
myButton.setLayoutParams(lp);
于 2013-01-25T19:41:40.110 回答