【问题标题】:GridLayout columns do not appear rightGridLayout 列显示不正确
【发布时间】:2017-04-06 03:01:15
【问题描述】:

我尝试配置 2 行 2 列的网格布局。但是这里的每个元素都占据了屏幕的整个宽度,而第二列在屏幕之外。请建议我在这里做错了什么。

<?xml version="1.0" encoding="utf-8"?>
<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:background="@color/colorTheme"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/moodsGridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />
    </GridLayout>

</LinearLayout>

【问题讨论】:

  • 对我来说似乎很明显,如果一列的宽度设置为match_parent,其他列将没有空间。尝试为您的 ImageButtons 使用离散 dp 值。
  • 由于元素被包围在GridLayout中,并且每个底层元素的行跨度仍然是1,所以它应该只占据屏幕的一半,对吧?如果我错了,请纠正我。
  • 试试看。也许我错了。
  • 我已经动态尝试过,结果符合预期。但是布局 xmls 造成了一些麻烦。
  • 然后保留动态生成的并将xml值设置为0dp或wrap_content

标签: android android-layout grid-layout


【解决方案1】:

试试这个:

用于 Gridlayout 中的统一宽度列和行:

android:layout_columnWeight="1"    
android:layout_rowWeight="1"

使用这个布局:

 <?xml version="1.0" encoding="utf-8"?>
<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:background="@color/color_white"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/moodsGridLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/textView2"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_row="1"
            android:layout_rowWeight="1"
            android:src="@mipmap/ic_launcher" />
    </GridLayout>

</LinearLayout>

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-20
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多