グリッドレイアウトを画面全体に表示したいので、プログラムでそれをやりたい 私はapp:layout_columnWeight
属性を追加することにより、静的なXMLでそれを行うと、それは私が次のようにしたいかを正確になります。Android GridLayout-画面全体をプログラムでカバー
しかし、私はプログラム的に同じレイアウトを作成しようとしたとき、それは次のようになります。
最初の画像と同じに見えます。ここに私のコードです:
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);
}
}
私を助けてください。私はここ21
Update- として分SDKを使用していますノート・ は私の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>
レイアウトcode.Youをすべて表示しますレイアウトのgridlayoutを使用せずに、他のgridlayoutをプログラムで作成しています。 – Drv
質問の更新 –
私はプログラムでXMLで書かれたレイアウト全体を作成したいと思います。 –