2017-02-10 89 views
0

我在Android Studio中按照我的應用程序的教程創建了可展開/可摺疊列表,但是我希望添加一個按鈕,該按鈕在標題展開時更改爲向上/向下按鈕的標題左側/坍塌。我如何添加這個?是如何將擴展/關閉圖標添加到我的應用程序?

的擴張和收縮的列表我的活動文件內容如下:

list_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:background="#f4f4f4" > 

<ExpandableListView 
    android:id="@+id/lvExp" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"/> 

</LinearLayout> 

list_group.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="8dp" 
android:background="#000000"> 


<TextView 
    android:id="@+id/lblListHeader" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
    android:textSize="17dp" 
    android:textColor="#f9f93d" /> 

</LinearLayout> 

list_item.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="55dip" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/lblListItem" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textSize="17dip" 
    android:paddingTop="5dp" 
    android:paddingBottom="5dp" 
    android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" /> 

</LinearLayout> 

我試圖通過改變list_group.xml的佈局文件,以實現它:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="8dp" 
android:background="#000000"> 


<selector xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <item android:drawable="@drawable/down_squared" android:state_checked="true" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
    <item android:drawable="@drawable/up_squared" android:state_checked="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
</selector> 

<TextView 
    android:id="@+id/lblListHeader" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
      android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft" 
    android:textSize="17dp" 
    android:textColor="#f9f93d" /> 

</LinearLayout> 

然而,網頁會崩潰,現在有設計選項卡說,有渲染錯誤,它們是:

以下類找不到: - 項目(修正構建路徑,編輯XML) - 選擇(修復構建路徑,編輯XML)

回答

1

使用複選框的背景設置爲您繪製

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/play_icon" android:state_checked="true" /> 
<item android:drawable="@drawable/pause_icon" android:state_checked="false" /> 
</selector> 

然後在代碼中設置一個回調複選框setOnCheckedChangeListener

onCheckedChanged裏面放expandGroupcollapseGroup電話,取決於檢查狀態

+0

嗨,存在list_main.xml從時更改默認圖標當我展開一個組時,反之亦然,是否有任何方法可以更改用於此圖標及其顏色的圖標?我看不到這個屬性。謝謝 – Spartan123

+1

是的,有一種方法。只需將@ drawable/down_squared和@ drawable/up_squared改爲別的。你可以用想要的顏色改變它的顏色,或者你可以使用setColorFilter來繪製顏色過濾器。順便說一句,你做錯了。你不應該把選擇器放在你的佈局中,而是把背景設置爲該選擇器的複選框。 – xklakoux

相關問題