简体   繁体   中英

How to change the background color of an ExpandableListView in Android?

The title of my question pretty much sums up what I want to ask so In the description I will describe my question in detail.

So I have an ExpandableListView and it is printed out on the screen in black with white text inside. When I press on the list it is expended and I can select the details in it. It looks something like this:

|--------------------------|
|black color               | 
|   Title text(in white)   |
|                          |
|__________________________|
|  Item 1  (black text with white background)
|  Item 2                  |
|  Item N...               |

So my question is how to change the background color of the inside of the "Text Title" and put a limit for the number of symbols displayed in the title?

Here are my XML files:

    <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="228dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#f4f4f4"
        android:orientation="vertical" >

        <ExpandableListView
            android:id="@+id/lvExp"
            android:layout_width="match_parent"
            android:layout_height="485dp"          >
        </ExpandableListView>
    </LinearLayout>
    </ScrollView>

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>    

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="#f4f4f4" 
    android:background="#323232"/>//this only changes some part of the background just around the text ONLY

</LinearLayout>

Try these properties in textview:

<TextView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:maxLines="1" 
    android:maxLength="10" 
    android:ellipsize="marquee"/>

In the expandablelistadapter : In getGroupView under

  if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.listheader, null);
        }

: put this :

 convertView.setBackgroundColor(Color.parseColor("#FFFFFF"));

And to limit the text title characters use in the XML:

<TextView 
android:id="@+id/secondLineTextView" 
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:maxLines="1" 
android:maxLength="10" 
android:ellipsize="end"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM