【问题标题】:GridLayout fill only one column leaving other columns emptyGridLayout 仅填充一列,其他列为空
【发布时间】:2017-07-12 03:17:13
【问题描述】:

我有一个GridLayout,里面有 3 列和随机数量的孩子,我希望每个孩子填写一列,而且只有一列,不多也不少。 但是下面的代码使TextViews 填充的不止这些。我希望每个 TextView 填满 1/3 的空间。

<android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:columnCount="3">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#abcdef"
        app:viewAspectRatio="3"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#fedcba"
        app:viewAspectRatio="3" />

</android.support.v7.widget.GridLayout>

如果 GridLayout 不可能,我很想知道其他可能性

【问题讨论】:

    标签: android grid-layout android-gridlayout


    【解决方案1】:

    我不太确定,但试试这个:

    <android.support.v7.widget.GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:columnCount="3">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#abcdef"/>
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#fedcba" />
    
    </android.support.v7.widget.GridLayout>
    

    或:

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="3">
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#abcdef"
        app:viewAspectRatio="1"/>
    
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_columnWeight="1"
        android:background="#fedcba"
        app:viewAspectRatio="1" />
    
    </android.support.v7.widget.GridLayout>
    

    希望对你有帮助

    【讨论】:

    • viewAspectRatio 不是TextView 或GridLayout 的属性。它是我刚刚忘记删除的自定义属性
    • android:layout_columnSpan="1" 尝试将此添加到您的 TextViews :) 像这样它只会占用您的 gridview 的一列
    猜你喜欢
    • 1970-01-01
    • 2014-10-09
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 2021-11-01
    • 1970-01-01
    相关资源
    最近更新 更多