注意:我承认标题有点模糊,但英语不是我的母语,我不确定如何用一句话描述问题。
背景
我正在尝试创建一个应用程序,显示所有应用程序信息在网格视图上。
网格视图的numColumns设置为auto_fit,以便其列数可以很好地适应各种屏幕。
问题
由于每个应用程序的信息有时可能很长,因此单元格的高度可能与相邻单元格不同。
我希望网格单元格的宽度相同(在所有情况下都有效),并且对于每一行,高度应该由该行单元格的最大高度确定。
这会导致发生奇怪的事情:
- 罕见:某些单元格变为空白。有时,在滚动和返回时,单元格会被填充...
- 相当普遍:某些单元格绘制在其他单元格上方。
- 相当普遍:某些单元格没有占用它们可以占用的空间,留下无法点击的空白空间。
- 非常罕见:在滚动时玩耍,如果你滚动到顶部,你会得到整个网格为空且无法滚动...
这里是一个截图,显示了#2和#3问题(它们通常不会一起显示):
我使用的代码
这是网格单元格的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/list_divider_holo_dark" >
<ImageView
android:id="@+id/appIconImageView"
android:layout_width="48dip"
android:layout_height="48dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:contentDescription="@string/app_icon"
android:src="@drawable/ic_menu_help" />
<TextView
android:id="@+id/appLabelTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/isSystemAppImageView"
android:layout_toRightOf="@+id/appIconImageView"
android:ellipsize="marquee"
android:text="label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="@+id/appDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/appLabelTextView"
android:layout_below="@+id/appLabelTextView"
android:layout_toLeftOf="@+id/isSystemAppImageView"
android:ellipsize="marquee"
android:text="description"
tools:ignore="HardcodedText" />
<ImageView
android:id="@+id/isSystemAppImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/appDescriptionTextView"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:contentDescription="@string/content_description_this_is_a_system_app"
android:src="@drawable/stat_sys_warning" />
</RelativeLayout>
我尝试过的方法
我尝试了多种解决方案:
使用HorizontalListView似乎可以解决这个问题,但它不允许单击/长按项目。
我找到了一个很好的库,可以使textViews具有“跑马灯”效果 (在这里),但它的许可证并不那么宽松。
将单元格高度设置为固定值或将textViews设置为具有固定行数也可以工作,但这会削减textViews的内容(部分),而无法显示它们。
我还尝试使用LinearLayout代替RelativeLayout,但没有帮助。
可能的解决方案是获取网格视图上所有视图使用的最大高度(使用getView在所有项上),类似于此代码,但它假定您知道列数,并且您确实希望所有单元格的高度相同。
问题
我应该如何处理这个问题?
我真的应该使用类似于StaggeredGrid的东西吗?
有没有一种方法可以使textViews保持固定大小,同时允许滚动(手动或自动)显示其内容?
layout_height="wrap_content"
设置吗? - Kuba Spatnyandroid:maxLines="4"
标签(或android:maxLength="10"
),还有android:ellipsize="marquee"
,但是在ListView中的问题是只有当特定视图处于焦点状态时才会滚动。因此,您需要创建一个扩展TextView的类,如此处所示:https://dev59.com/UHI-5IYBdhLWcg3wfoa1 - Kuba Spatny