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:
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.
Update- Here is my xml code
<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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.