如何将扩展/关闭图标添加到我的应用程序?

问题描述:

我在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)

使用复选框的背景设置为您绘制

<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