[英]Android GridLayout- cover whole screen programmatically
I want grid layout to cover whole screen and I want to do it programmatically. 我希望网格布局能够覆盖整个屏幕,并且我需要以编程方式进行。 When I do it in static xml by adding
app:layout_columnWeight
attribute, it looks exactly which I want to have as follows: 当我通过添加
app:layout_columnWeight
属性在静态xml中执行此操作时,它看起来完全符合我的要求,如下所示:
But when I tried to create same layout programmatically, it looks like this: 但是,当我尝试以编程方式创建相同的布局时,它看起来像这样:
I want it to look same as first image. 我希望它看起来与第一张图片相同。 Here is my code:
这是我的代码:
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);
}
}
Please help me. 请帮我。 Note- I am using min sdk as 21.
注意-我将min sdk用作21。
Update- Here is my xml code 更新-这是我的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>
Replace your activity with: 将您的活动替换为:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.YOUR_LAYOUT_NAME);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.