我正在向 a 添加一定数量的按钮,GridLayout
并且我不想设置特定的列数量(因为当我这样做并且手机的方向改变时,事情看起来不太好)。我试过取出列规范,但这只会导致它有 1 个非结束行。
我希望按钮的大小相同,并让它们填充可用的空白空间。我对拉伸按钮的宽度不感兴趣。
爪哇:
GridLayout gridLayout = (GridLayout) findViewById(R.id.layout_grid);
for(int i = 0; i < numButtons; i++)
{
Button button = new Button(this);
button.setText(i+"");
gridLayout.addView(button);
}
XML(我将其包装GridLayout
在 a 中,ScrollView
因为我预计按钮将垂直从屏幕上消失):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollview_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
tools:context=".Grid" >
<GridLayout
android:id="@+id/layout_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
</GridLayout>
</ScrollView>