我的问题很简单,我正在尝试动态更改此按钮的宽度:
按钮> android:layout_height =“35dip”android:background =“@ drawable / buttonlesson”android:text =“Level 3 [TAP HERE]”android:onClick =“MenuLI1L3”android:layout_width =“300dp”android:id = “@ id / II3”> /按钮“
这是我使用的代码:
Button myButton = (Button) findViewById(R.id.II3);
myButton.setWidth(10);
myButton.setText("kl");
文字确实改变但不是宽度 . 声音就像有一个bug . 右边有另一个按钮,当这个按钮缩小到10像素时,可以填补间隙,所以我也无法改变上面的Linearlayout .
任何解释和解决方案?它不应该工作吗?谢谢
2 回答
我认为wrap_content对你的具体情况不起作用,对吗?如果您需要绝对宽度,则需要通过新的LayoutParameters分配,即
其中someDensityFactor是你的屏幕密度(浮点数) . 您也可能需要使布局无效,以便重新绘制按钮 .
作为建议尝试在父视图上调用invalidate()(这将导致对所有子项的绘图调用 - 包括您的按钮) . 这可能不起作用,因为您还需要按钮重新运行其onMeasure()逻辑(在绘图之前运行) .
使用无效或任何其他方法进行播放,这将导致父级调用子级的onMeasure .