我有一些线性布局的按钮.按钮的宽度取决于设备,因为它们水平地填满屏幕.我想要有方形按钮,这是我的问题.有人可以帮助我吗?
<Button android:id="@+id/b_0xa" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:clickable="false" android:text="@string/b_0xa" />
动态获取按钮的宽度[使用int x = mButton.getWidth()],然后使用返回的值设置高度[mButton.setHeight(x)].
附: :建议使用LayoutParams设置任意视图的高度和宽度.所以,而不是使用setHeight(),你应该使用setLayoutParams().
mButton.setLayoutParams(new LinearLayout.LayoutParams(x,x));