GridView视图的高度未包含所有水平项目

时间:2018-08-22 14:41:54

标签: java android android-layout gridview kotlin

gridview项之一具有更高的高度,并且没有被gridview包裹

enter image description here

但是,如果我先放它,然后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>

有什么建议吗?

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

尝试像这样将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" />

答案 2 :(得分:-1)

尝试从

更改
 <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" />