如何在可扩展ListView中向子元素添加不同的图标?

时间:2017-02-24 22:33:13

标签: android android-layout listview

我已按照本教程为我的应用创建了一个Expandable ListView:

http://www.androidhive.info/2013/07/android-expandable-list-view-tutorial/

我想为每个与右边对齐的孩子添加不同的图标,我该怎么做?

感谢任何帮助,谢谢

1 个答案:

答案 0 :(得分:1)

在您发布的文章的list_item.xml中,只需将所需的drawable放在TextView的右侧:

<?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:drawableRight[End]="@drawable/my_cool_drawable"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>

如果每个项目都有不同的drawable,那么只需使用setCompoundDrawable方法在运行时设置它:

txtListChild.setCompoundDrawable(null, null, myCoolDrawable, null);

您可能还想指定drawablePadding。顺便说一句,文章示例中的LinearLayout没用,您可以使用FrameLayout代替merge标记。