Ich möchte Rasterlayout den gesamten Bildschirm abdecken und ich möchte es programmgesteuert tun. Wenn ich es in statischen xml tun durch Hinzufügen app:layout_columnWeight
Attribut, es sieht genau, welche ich wie folgt haben wollen:Android GridLayout-decken ganzen Bildschirm programmgesteuert
Aber wenn ich programmatisch gleiche Layout erstellen versucht, sieht es wie folgt aus:
Ich möchte, dass es wie das erste Bild aussieht. Hier ist mein 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);
}
}
Bitte helfen Sie mir. als 21.
Update- Hier Note- I min sdk bin mit meinem xml-Code ist
<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>
Zeigen Sie Ihr Layout code.You sind Wenn Sie das Layout des Layouts nicht verwenden, erstellen Sie programmatisch ein anderes Rasterlayout. – Drv
Aktualisiert in Frage –
Ich möchte das gesamte Layout in Xml programmgesteuert erstellen. –