【问题标题】:Android ListView cutting off items wider than the first itemAndroid ListView 截断比第一个项目更宽的项目
【发布时间】:2017-06-30 17:57:34
【问题描述】:

我在 Horizo​​ntalScrollView 中有一个 Listview。列表视图保存文件路径,水平滚动视图旨在消除对较长路径的包装。

一切都按预期工作,除了列表视图中比第一项长的任何项目都在第一项的长度处被剪切(无论第一项的宽度如何,其他项目都不会滚动超过那个宽度)。

这是给我带来麻烦的布局部分:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layoutListView"
    android:orientation="vertical"
    android:layout_below="@+id/spFileTypes">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/lvItems"/>

    </HorizontalScrollView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:id="@+id/tvStatus"/>

</LinearLayout>

这是问题的截图:

图像一直滚动到右侧。第三项是切断“.php”。我已经尝试过使用长文件名和短文件名、许多列表(高位两位数)和只有少数的列表,在每种情况下,宽度限制似乎都取决于第一项的宽度。

每当列表发生变化时,它总是将其宽度限制为第一个元素的宽度。

期望的行为是让它与最宽项目的宽度相匹配。

我用谷歌搜索了我的心,翻阅了官方文档,但我很难过。非常感谢任何帮助:)

【问题讨论】:

  • 您的 Horizo​​ntalScrollView 的 layout_weight = 1 而 LinearLayout 中的第二个孩子没有。您应该将权重添加到 TextView 或将其从 ListView 中删除。希望对您有所帮助。
  • 我会记下这一点,但 TextView 完全按照需要显示在布局中(截屏只是为了显示截断行为)

标签: android android-layout listview


【解决方案1】:

不要将 ListView 包装在 Horizo​​ntalScrollView 中,试试这个:

<ListView
    android:id="@+id/listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="horizontal"/>

【讨论】:

  • 是的,列表视图不应该是任何滚动视图的子视图。它可能导致奇怪的行为。
  • 不知道为什么,但是当我将代码更改为该代码时,它会包装字符串,使它们有点难以阅读。我也没有在屏幕上看到任何滚动条(我假设是因为它们正在包装?)这可能是每个单独列表项的 TextView 的问题吗?
猜你喜欢
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多