我知道如何使用 Layout inflator 制作自定义 ListView ,以及如何为 ListView 的行使用 xml 布局。但我想知道如何在 ListView 行上应用不同的样式,如下所示。
最佳答案
这是listview_item_shadow.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/darker_gray" />
</shape>
</item>
<item
android:right="1dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
</shape>
</item>
</layer-list>
现在您应该在 ItemView 的选择器中使用它! - listview_item_backgroundstate.xml
您需要将 listview_item_backgroundstate.xml
设置为 ListView 项目的背景
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@android:color/transparent"></item>
<item android:state_selected="true" android:drawable="@android:color/transparent"></item>
<item android:state_focused="true" android:drawable="@android:color/transparent"></item>
<item android:drawable="@drawable/listview_item_shadow"></item>
</selector>
最后,您必须像在 ListView 中一样设置 custom_selector.xml。 android:listSelector="@drawable/custom_selector.xml"
<?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:drawable/color/white" />
<item
android:state_pressed="true"
android:drawable="@drawable/pressed_background_blue" />
</selector>
或者使用那种图像作为每个列表项的背景..
关于android - 带有 shodows 的自定义 ListView 到 android 中的 ListView 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19246835/