Gridview 视图高度不包含所有水平项目
Gridview view height is not wrapping all horizontal items
其中一个 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>
有什么建议吗?
尝试像这样将 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" />
尝试改变
<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" />
其解
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
其中一个 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>
有什么建议吗?
尝试像这样将 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" />
尝试改变
<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" />
其解
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