需要将四个按钮分布在四方形的表布局中,button如下:
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:background="@drawable/tile_selector"
android:text="Button1" />
运行很好,现在我想获取每个button的宽度然后设置它的高度。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
for (int i = 0; i <= 4; i++) {
String buttonID = "button" + i;
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
Button b = (Button) findViewById(resID);
int width = b.getWidth();
b.setHeight(width);
}
}
但是却崩溃了,不知道为什么?