2016-07-11 4 views
2

Я хочу, чтобы сетка была покрыта всем экраном, и я хочу сделать это программно. Когда я делаю это в статическом XML, добавив app:layout_columnWeight атрибут, он выглядит точно так, который я хочу иметь следующее:Android GridLayout- охватывает весь экран программно

With xml

Но когда я попытался создать такую ​​же схему программно, это выглядит следующим образом:

programmatically

Я хочу, чтобы это было похоже на первое изображение. Вот мой код:

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); 
} 
} 

Пожалуйста, помогите мне. Примечание- Я использую мин 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> 
+0

Показать макет code.You являются не используя gridlayout вашего макета, вы создаете какой-то другой gridlayout программно. – Drv

+0

Обновлено в вопросе –

+0

Я хочу создать целую макет, написанную в xml программно. –

ответ

-1

Заменить вашу деятельность с:

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.YOUR_LAYOUT_NAME); 

} 
} 
+0

Макет, который я создал с помощью xml, работает нормально. Я использовал тот же код, который вы разместили здесь. Но я хочу создать такой же макет программно, и мне нужна помощь для этого. –

+0

ОК, я понял. – Drv

Смежные вопросы