Android ListView(Selector 顏色)

qingyezhu發表於2014-04-26

listview_color.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="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listViewColor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:choiceMode="singleChoice"
        android:divider="@android:color/darker_gray"
        android:dividerHeight="1dp" >
    </ListView>

</LinearLayout>

 

 

aa.java

package com.wangzhu.demoselector;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class ListViewColorActivity extends Activity {

    private ListView listViewColor;

    private ArrayAdapter<String> colorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_color);
        initView();
    }

    private void initView() {
        listViewColor = (ListView) findViewById(R.id.listViewColor);
        colorAdapter = new ArrayAdapter<String>(this, R.layout.list_item_color,
                R.id.item_tv, getResources().getStringArray(
                        R.array.listviewDatas));
        listViewColor.setAdapter(colorAdapter);
    }
}

list_item_color.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="match_parent"
    android:background="@drawable/list_item_bg_color_selector"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/item_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

list_item_bg_color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/green" android:state_pressed="true"/>
    <item android:drawable="@color/red" android:state_activated="true"/>
    <item android:drawable="@color/white"/>

</selector>

 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string-array name="listviewDatas">
        <item>星期一</item>
        <item>星期二</item>
        <item>星期三</item>
        <item>星期四</item>
        <item>星期五</item>
        <item>星期六</item>
        <item>星期日</item>
    </string-array>

</resources>

 

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="black">#000000</color>
    <color name="red">#ff0000</color>
    <color name="green">#00ff00</color>
    <color name="white">#ffffff</color>

</resources>

 

 

 

相關文章