【问题标题】:Make only a part of ListView element change when clicked单击时仅更改 ListView 元素的一部分
【发布时间】:2013-12-20 22:04:28
【问题描述】:

我发现很难向搜索引擎或任何人解释我的问题。我会尽力解释。我有一个 ListView,我为 ListView 中的元素制作了自定义视图。这是 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="@dimen/list_item_height"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:singleLine="true"
    android:ellipsize="marquee"
    android:textColor="@color/black"
    android:gravity="center"
    android:background="@drawable/spinner_dropdown_style"/>

<View
    android:id="@+id/divider"
    android:layout_width="fill_parent"
    android:layout_height="5dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/text1"
    android:background="@drawable/shadow" />

</RelativeLayout>

这就是 ListView 的样子:

如您所见,分隔线始终为灰色,但当单击 ListView 元素时,分隔线变为蓝色。这就是问题。我只希望元素的白色部分变为绿色,灰色分隔线保持灰色。

这是 spinner_dropdown_style(选择器):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"
    android:drawable="@android:color/transparent" />
<item android:state_pressed="true"
    android:drawable="@color/button_color_pressed" />
</selector>

如何使选择器仅在白色区域起作用,而灰色分隔线即使在单击元素时也保持灰色?

【问题讨论】:

    标签: java android xml android-listview


    【解决方案1】:

    在你的列表视图中添加android:divider="@layout/id of layout"这一行

    制作另一个布局

    <View android:id="@+id/divider" android:layout_width="fill_parent" android:layout_height="5dp" android:layout_alignParentLeft="true" android:layout_below="@+id/text1" android:background="@drawable/shadow" />

    添加删除元素。

    希望这会有所帮助

    【讨论】:

    • 谢谢!解决了我的问题,您的解决方案中只有一个错误的细节。分隔符不应该是布局文件。它应该是可绘制的或颜色。当我添加一个布局文件作为分隔符时,应用程序崩溃了。
    【解决方案2】:

    在 xml 中添加到您的列表视图:

    android:drawSelectorOnTop="false"
    android:cacheColorHint="#00000000"
    

    【讨论】:

      【解决方案3】:

      ListView 有自己的选择器,它适用于选定的项目。您只是切换 TextView 的背景,而不是整个选定项目。为此,您可以尝试将其添加到您的列表视图中:

      android:drawSelectorOnTop="true"
      android:listSelector="@color/button_color_pressed"
      

      【讨论】:

        猜你喜欢
        • 2015-11-22
        • 2016-12-09
        • 1970-01-01
        • 2023-03-19
        • 2016-09-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-23
        相关资源
        最近更新 更多