[英]Android - ExpandableListView change background for child items
我在我的應用程序中使用ExpandableListView
,我從用戶那里得到的一個抱怨是,當List
項目被展開時,很難在視覺上區分子項目結束和下一個組項目開始的位置。
所以我想將子List
項的背景更改為不同的陰影。
到目前為止,我所做的殘酷嘗試都是基於直接更改子View
項目中元素的背景顏色和文本,但這會導致丟失和突出顯示。 所以我的問題是 - 實現上述目標的好策略是什么?
我嘗試了styles
和selectors
但真正讓我感到困惑的是,如果我更改了子項目的背景,那么我需要為焦點/啟用等所有組合添加selectors
。當我試圖覆蓋一件事時。
是否有一種方法可以繼承父style
並僅為非重點啟用的子項設置background
,並保留其他styles
?
好。 這對我有用:
res/drawable
目錄中創建list_background.xml ExpandableListAdapter#getChildView
創建可擴展列表的子視圖時使用的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>
我不得不復制list_highlight_active.xml
和list_highlight_inactive.xml
從/android-sdk-windows-1.6_r1/platforms/android-1.5/data/res/drawable
到我的項目的繪制目錄。 @color/item_body
只是一片灰色
我尋找類似的解決方案。 您可以嘗試在適配器方法getChieldView上獲取父View。 我認為這個父母是你所有chields的父視圖。 並將資源背景設置為此父級。 我這樣做:
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fa_report_sub_item, null);
View parentGroup=(View) convertView.getParent();
parentGroup.setBackgroundResource(R.drawable.fa_summ_report_main_item);
}
我剛剛發現了一種“有點”設置背景顏色而無需跳過選擇器箍的方法。
正如您所注意到的,將背景設置為純色會消除選擇器高光,因為新的背景顏色會遮擋它。 Droidin有什么是正常的解決方案:為您自己的背景選擇器提供您想要的確切顏色。 至少可以說是痛苦的。
但如果你想要的只是一點色差,那么有一種更簡單的方法:alpha混合。 使用Alpha值設置背景顏色。 例如,將background設置為“#BBFFFFFF”。 前兩位數字表示alpha級別。 它將全部混合,因此背景將不是純白色,選擇高光將不是正常的亮橙色,但孩子將是不同的顏色,亮點仍然有效。 雙贏。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.