gpt4 book ai didi

android - 具有自定义背景的可选择/可突出显示的列表项

转载 作者:搜寻专家 更新时间:2023-11-01 08:56:52 24 4
gpt4 key购买 nike

我有一个 ListView 项目,它有一个设置的背景。这会覆盖在按下/选择项目时出现的默认蓝色突出显示。有没有办法同时拥有背景和选择器?

这是我合并背景和选择器的尝试...

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true" android:color="@color/red"/>

</selector>
<item>
<shape
android:dither="true"
android:shape="rectangle" >

<solid android:color="#ccc" />
</shape>
</item>
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="6dp" />

<solid android:color="@android:color/white" />

</shape>
</item>

</layer-list>

这是在我的 drawable 文件夹中,我在我的 ListItem xml 中设置了它:

android:background="@drawable/my_background

最佳答案

要拥有自定义背景和默认选择器效果(按下/选择时会出现另一个绘图效果)有点困难,经过几次尝试,我做到了。

您应该在单独的 xml 文件中定义两个选择器:listitem_background.xmllistitem_selector.xml

第一个用于list item的背景,在item被按下和正常状态下产生效果。

第二个用于列表的选择器,它通过将所有状态设置为透明来摆脱 ListView 的默认选择器。

默认选择器效果定义在第一个xml文件:listitem_background.xml。

首先你需要一个 xml 文件来定义一些可绘制的颜色:color_drawable.xml,在 res/values 目录中:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- The color of the normal state. -->
<drawable name="listitem_normal">#E671B8</drawable>
<!-- The two color below show when the item is pressed, you should change that to the color you want. -->
<drawable name="listitem_pressed">#e7eeab</drawable>
<drawable name="listitem_selected">#e7eeab</drawable>

</resources>

然后,res/drawable中的listitem_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/listitem_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/listitem_selected" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/listitem_normal"/>

</selector>

listitem_selector.xmlres/drawable 中:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@color/android:transparent" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@color/android:transparent"/>

</selector>

将 listitem_background 设置为 listitem:

<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/listitem_background" >

...

</RelativeLayout>

将 listitem_selector 设置为 ListView :

<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@drawable/listitem_selector" />

关于android - 具有自定义背景的可选择/可突出显示的列表项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351644/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com