【发布时间】:2016-11-13 09:44:41
【问题描述】:
我希望网格布局覆盖整个屏幕,并且我想以编程方式进行。
当我通过添加 app:layout_columnWeight 属性在静态 xml 中执行此操作时,它看起来正是我想要的如下:
但是当我尝试以编程方式创建相同的布局时,它看起来像这样:
我希望它看起来与第一张图片相同。这是我的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
android.support.v7.widget.GridLayout gridLayout = new android.support.v7.widget.GridLayout(this);
gridLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
gridLayout.setColumnCount(3);
for (int i = 0; i < 9; i++) {
Button button = new Button(MainActivity.this);
GridLayout.LayoutParams lp = (GridLayout.LayoutParams) button.getLayoutParams();
if (lp == null) {
lp = new GridLayout.LayoutParams();
}
lp.columnSpec = GridLayout.spec(GridLayout.UNDEFINED, 1, 1f);
button.setLayoutParams(lp);
button.setText("Button " + (i + 1));
gridLayout.addView(button);
}
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(gridLayout);
setContentView(linearLayout);
}
}
请帮助我。 注意-我使用 min sdk 作为 21。
更新- 这是我的xml代码
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="3">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_columnWeight="1"
app:layout_gravity="fill"
android:text="Button 9" />
</android.support.v7.widget.GridLayout>
</LinearLayout>
【问题讨论】:
-
显示您的布局代码。您没有使用布局的网格布局,而是以编程方式创建其他一些网格布局。
-
已更新
-
我想以编程方式创建用 xml 编写的整个布局。
标签: android android-gridlayout