【问题标题】:Gridview view height is not wrapping all horizontal itemsGridview 视图高度不包含所有水平项目
【发布时间】:2018-08-22 14:41:53
【问题描述】:

其中一个 gridview 项目的高度更大,并且不被 gridview 包裹

但是如果我先放上它,然后 gridview 将它包裹起来,以防最后一列中的大元素出现问题。

网格视图

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menuListFilter"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:isScrollContainer="false"
    android:stretchMode="columnWidth" />

物品

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/itemMenuContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/itemMenuFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:textSize="@dimen/large_font"
        android:textColor="@color/colorDarkGrey"
        android:textStyle="bold" />

    <com.fifth_llc.siply.main.views.Line
        android:id="@+id/itemMenuSelected"
        android:layout_width="15dp"
        android:layout_height="wrap_content"
        app:underlineHeight="2dp"
        app:underlineColor="@color/colorOrange" />

</LinearLayout>

有什么建议吗?

【问题讨论】:

    标签: java android android-layout gridview kotlin


    【解决方案1】:

    解决办法

    class FullHeightGridView : GridView {
    
        constructor(context: Context) : super(context) {}
    
        constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}
    
        constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}
    
        override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
            super.onLayout(changed, l, t, r, b)
            if (adapter != null) {
                var i = 0
                while (i < childCount) {
                    // Determine the maximum height for this row
                    var maxHeight = 0
    
                    for (j in i until i + numColumns) {
                        val view = getChildAt(j)
                        if (view != null && view.height > maxHeight) {
                            maxHeight = view.height
                        }
                    }
                    //Log.d(TAG, "Max height for row #" + i/numColumns + ": " + maxHeight);
    
                    // Set max height for each element in this row
                    if (maxHeight > 0) {
                        for (j in i until i + numColumns) {
                            val view = getChildAt(j)
                            if (view != null && view.height != maxHeight) {
                                view.minimumHeight = maxHeight
                            }
                        }
                    }
    
                    layoutParams.height = maxHeight
    
                    i += numColumns
                }
            }
        }
    }
    

    并在 xml 中使用 FullHeightGridView 而不是 GridView

    【讨论】:

      【解决方案2】:

      尝试像这样将android:minLines=""android:maxLines = "" 设置为您的TextView

       <TextView
          android:id="@+id/itemMenuFilter"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:padding="8dp"
          android:textSize="@dimen/large_font"
          android:textColor="@color/colorDarkGrey"
          android:minLines="2"
          android:maxLines="2"
          android:textStyle="bold" />
      

      【讨论】:

      • 现在高度扩大了,但这对我来说不是解决方案,因为数据动态和最后一个文本可以是前三个这样的短文本
      【解决方案3】:

      尝试改变

       <com.fifth_llc.siply.main.views.Line
          android:id="@+id/itemMenuSelected"
          android:layout_width="15dp"
          android:layout_height="wrap_content"
          app:underlineHeight="2dp"
          app:underlineColor="@color/colorOrange" />
      

       <com.fifth_llc.siply.main.views.Line
          android:id="@+id/itemMenuSelected"
          android:layout_width="wrap_content"
          android:layout_height="1dp"
          app:underlineHeight="2dp"
          app:underlineColor="@color/colorOrange" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-29
        • 1970-01-01
        • 1970-01-01
        • 2017-05-25
        • 1970-01-01
        • 2017-07-06
        • 2018-12-23
        • 1970-01-01
        相关资源
        最近更新 更多