GridLayout列宽

bwo*_*gie 44 android android-layout android-gridlayout

我的帖子里有两列GridLayout.我想要做的是让这些列占据屏幕宽度的一半,然后让它的子内容填充它们自己的单元格宽度/高度.我尝试设置子项,fill_parent但这只会导致第一个接管整个布局.似乎GridLayout不支持weight?也许有更好的布局可供使用,但我想要一个网格样式布局,这似乎是自然的选择.

Tob*_*iug 54

此代码可在API21之前的支持库中获得!

我有一段简单的代码来显示4列gridLayout中的4个按钮占用50%的可用空间:也许它可以帮助我

<GridLayout
    android:id="@+id/grid"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="2"
    >


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />

       <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:layout_gravity="fill"
        android:layout_columnWeight="1"
        />



</GridLayout>
Run Code Online (Sandbox Code Playgroud)

解决方案可能就是:

android:layout_gravity="fill"
android:layout_columnWeight="1"
Run Code Online (Sandbox Code Playgroud)

  • 我现在可以使用支持库最新版本的一个预备API 21.只需添加依赖项 - com.android.support:gridlayout-v7:22.0.0 (19认同)
  • 根据我的权重工作经验,你必须将`layot_width`设置为`0dp` @NigamPatro希望这有助于 (7认同)

Meh*_*glu 13

对于API 21之前的版本,请使用支持库:

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
Run Code Online (Sandbox Code Playgroud)

你的依赖.

然后在你的xml文件中:

<android.support.v7.widget.GridLayout
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:columnCount="2"
                    app:orientation="horizontal"
                    app:rowCount="1">

                    <TextView
                        android:text="1"
                        android:textStyle="bold"
                        app:layout_columnWeight="1"
                        />

                    <TextView
                        android:text="2"
                        android:textStyle="bold"
                        app:layout_columnWeight="1" />

</android.support.v7.widget.GridLayout>
Run Code Online (Sandbox Code Playgroud)

这里注意使用"app"前缀,别忘了添加

xmlns:app="http://schemas.android.com/apk/res-auto"
Run Code Online (Sandbox Code Playgroud)

到你的xml文件


Nar*_*lle 6

在占据 50% 可用空间的 2 列网格布局中动态添加视图:

GridLayout gridLayout = new GridLayout();

View view; //it can be any view

GridLayout.LayoutParams param = new GridLayout.LayoutParams();

param.columnSpec = GridLayout.spec(GridLayout.UNDEFINED,GridLayout.FILL,1f);

param.width = 0;

view.setLayoutParams(param);

gridLayout.add(view);
Run Code Online (Sandbox Code Playgroud)

以静态方式(在 .xml 文件中)。

<android.support.v7.widget.GridLayout
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   app:alignmentMode="alignBounds"
   app:columnCount="2"
   app:columnOrderPreserved="false"
   app:orientation="horizontal"
   android:layout_margin="20dp"
   android:layout_marginBottom="30dp"
   android:padding="4dp"
   app:rowCount="2">

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_username"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/user_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_first_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_row="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    android:text="@string/first_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_middle_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="0"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/middle_name" />

<TextView
    android:layout_marginTop="2dp"
    android:id="@+id/edit_profile_last_name" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_column="1"
    app:layout_gravity="fill"
    app:layout_columnWeight="1"
    app:layout_row="1"
    android:text="@string/last_name" />

</android.support.v7.widget.GridLayout>
Run Code Online (Sandbox Code Playgroud)


bwo*_*gie 5

好吧,所以我放弃了网格视图,只使用了一些线性布局.我做了一个垂直的,然后添加了两个水平.它比网格视图稍微复杂一些......但直到我发现它至少可以解决这个问题.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/btn_mybutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionmybutton"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/btn_prefs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:background="@color/pomegranate"
            android:contentDescription="@string/contentDescriptionSettings"
            android:src="@drawable/ic_settings" />

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

然后我添加这个使按钮正方形:)

@Override
 public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);

  btnPrefs.setMinimumHeight(btnPrefs.getWidth());
  btnVerse.setMinimumHeight(btnMyButton.getWidth());

 }
Run Code Online (Sandbox Code Playgroud)