我想在Table布局方块中制作4个按钮。
按钮看起来像那样(id最多可以计算4个):
<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的witdh并将该值设置为它的高度。
我的代码:
@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);
}
}
但那会崩溃......为什么?
请帮帮我。
答案 0 :(得分:2)
尝试按下面的按钮
Button[] buttons;
for(int i=0; i<4; i++) {
{
String buttonID = "button" + (i+1);
int resID = getResources().getIdentifier(buttonID, "id", getPackageName());
buttons[i] = ((Button) findViewById(resID));
//set your height and width as you are doing.
}
希望它有效......