【问题标题】:Android GridLayout- cover whole screen programmaticallyAndroid GridLayout - 以编程方式覆盖整个屏幕
【发布时间】:2016-11-13 09:44:41
【问题描述】:

我希望网格布局覆盖整个屏幕,并且我想以编程方式进行。 当我通过添加 app:layout_columnWeight 属性在静态 xml 中执行此操作时,它看起来正是我想要的如下:

但是当我尝试以编程方式创建相同的布局时,它看起来像这样:

我希望它看起来与第一张图片相同。这是我的代码:

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

请帮助我。 注意-我使用 min 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>

【问题讨论】:

  • 显示您的布局代码。您没有使用布局的网格布局,而是以编程方式创建其他一些网格布局。
  • 已更新
  • 我想以编程方式创建用 xml 编写的整个布局。

标签: android android-gridlayout


【解决方案1】:

将您的活动替换为:

 public class MainActivity extends AppCompatActivity {

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

}
}

【讨论】:

  • 我使用 xml 创建的布局工作正常。我使用了与您在此处发布的代码相同的代码。但我想以编程方式创建相同的布局,我需要帮助。
猜你喜欢
  • 2019-11-02
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多