Android-ExpandableListView更改子项的背景

Bos*_*one 4 customization android background expandablelistview

ExpandableListView在我的应用程序中使用的方法是,从用户那里得到的投诉之一是,当List展开该项目时,很难从视觉上区分子项目结束的位置和下一个分组项目的开始位置。

因此,我想将子List项的背景更改为不同的阴影。

到目前为止,我所做的残酷尝试都是基于直接更改子View项中元素的背景颜色和文本,但这会导致悬停和突出显示丢失。所以我的问题是-实现上述目标的最佳策略是什么?

我尝试过stylesselectors但是真正让我感到困惑的是,如果我更改子项的背景,那么我需要添加selectors所有焦点/启用等的组合。当我试图这样做时,它会覆盖一件事。

有没有一种方法可以继承父项,style并且background只为未聚焦的已启用子项设置并styles保留其他项?

Bos*_*one 5

好。这对我有用:

  1. 在您的项目res/drawable目录中创建list_background.xml
  2. 将子项的顶层布局的背景值设置为上方的可绘制对象。如果您此时不敢恭维-我指的是在以下位置创建可扩展列表的子视图时使用的xml布局文件ExpandableListAdapter#getChildView

这是完整的可绘制文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:state_enabled="true"
        android:drawable="@drawable/list_highlight_active" />
    <item android:state_enabled="true" android:state_selected="true" 
        android:state_window_focused="true"
    android:drawable="@drawable/list_highlight_inactive" />
    <item android:state_enabled="true" android:state_window_focused="true" 
        android:drawable="@color/item_body" />
    <item android:drawable="@color/item_body" />
</selector>
Run Code Online (Sandbox Code Playgroud)

我不得不复制list_highlight_active.xmllist_highlight_inactive.xml/android-sdk-windows-1.6_r1/platforms/android-1.5/data/res/drawable我的项目的绘制目录。@color/item_body只是灰色阴影