动态改变 ImageButton 的权重

Dynamically change weight of an ImageButton

我在 LinearLayout 中的相对布局内有一个线性布局我有两个图像按钮,每个按钮的权重为 50,我想使用 java

将一个 ImageButton 的权重设置为 100

试试这个来动态设置 weight

LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) imagebutton.getLayoutParams();
layoutParams.weight = 1.0f;
imagebutton.setLayoutParams(layoutParams);

更多详细信息请参阅此内容How to set layout_weight attribute dynamically from code?

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

p.weight = 1;   
imagebutton.setLayoutParams(p);