【发布时间】:2014-01-18 12:14:36
【问题描述】:
注意:好的,我承认标题有点含糊,但英语不是我的主要语言,我不知道如何用一句话来描述这个问题。
背景
我正在尝试创建一个在 gridView 上显示所有应用信息的应用。
gridView 将其 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 具有“字幕”效果 (here),但它的许可证并不那么宽松。
-
将单元格高度设置为固定值或将 textViews 设置为固定行数也可以,但随后会减少(部分)textViews 的内容而无法显示它们。
我也尝试过使用 linearLayouts 而不是 RelativeLayout,但没有帮助。
一种可能的解决方案是获取 gridView 上所有视图使用的最大高度(在所有项目上使用 getView),类似于 this code,但它假设您知道列数,并且您知道希望所有单元格的高度相同。
问题
我应该如何处理?
我真的应该使用 StaggeredGrid 之类的东西吗?
有没有办法让 textViews 保持固定大小,但允许它们使用滚动(手动或自动)显示其内容?
【问题讨论】:
-
你试过设置
layout_height="wrap_content"的封闭相对布局吗? -
@KubaSpatny 是的。可悲的是,它没有帮助。 :(
-
好吧 StaggeredGrid 肯定会工作,但它可能是一个矫枉过正。您的意思是使用选框效果库,您是否尝试过自己实现它?
-
我没有,因为我不确定我应该怎么做以及需要多长时间才能做到。我在想也许它应该从 HorizontalScrollView 扩展,但它不会是可触摸的。我还需要对其进行足够的定制以告诉它如何移动。
-
TextView 和
android:ellipsize="marquee"有android:maxLines="4"标签(或android:maxLength="10"),但是listview 的问题是它只有在特定视图处于焦点时才会滚动。因此,您必须创建一个扩展 TextView 的类,如此处所示stackoverflow.com/questions/1827751/…
标签: android textview android-gridview marquee