实现ListView背景
ListView
这里实现了1、点击ListView中的Item都会改变此Item的背景2、使用Theme自定义Item选择器,也就是当选中一个item的时候,此item的背景改变
文件说明:
ListDemo.java -------Activity类
res/layout/list.xml ------布局文件
res/layout/listitem.xml ------每一个item的布局
res/drawable/addtion.png -----随便找的做,用做点击item时候设置背景的图片
res/drawable/icon.png ----随便找的图片,用于选择器
res/values/styles.xml ------风格样式表文件
ListDemo.java
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
res/layout/list.xml
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
res/layout/listitem.xml
res/values/styles.xml
--------------------------------------------------------------------------------
图片放进去有点丑陋,这里仅仅做为演示(演示结果为我写的另一个例子)。
ListView背景:
[img]http://dl.iteye.com/upload/attachment/351367/bf716d98-c7ce-37a9-9a38-a7a8cee97b7a.jpg[/img]
点击item设置的背景:
[img]http://dl.iteye.com/upload/attachment/351369/681928eb-bc49-3058-bc9f-0e5f27aa3814.jpg[/img]
ListView
这里实现了1、点击ListView中的Item都会改变此Item的背景2、使用Theme自定义Item选择器,也就是当选中一个item的时候,此item的背景改变
文件说明:
ListDemo.java -------Activity类
res/layout/list.xml ------布局文件
res/layout/listitem.xml ------每一个item的布局
res/drawable/addtion.png -----随便找的做,用做点击item时候设置背景的图片
res/drawable/icon.png ----随便找的图片,用于选择器
res/values/styles.xml ------风格样式表文件
ListDemo.java
--------------------------------------------------------------------------------
package cc.androidos.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
*
* @author Wang XinFeng
* @version 1.0
* @since 2009-3-24
*/
public class ListDemo extends Activity
{
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
//如果你设置了此项,那么就会改变listView 的选择器
setTheme( R.style.theme );
setContentView( R.layout.list );
final ListView lv = ( ListView ) findViewById( R.id.ListView01 );
ArrayAdapter<String> ss = new ArrayAdapter<String>(this,R.layout.listitem,new String[]{"A","B"});
lv.setAdapter( ss );
lv.setOnItemClickListener( new AdapterView.OnItemClickListener(){
//点击item的事件监听器
@Override
public void onItemClick( AdapterView<?> arg0, View arg1, int arg2,
long arg3 )
{
//arg1实际上就是你点击的那个item的组件对象
//在这里直接设置它的背景
arg1.setBackgroundResource( R.drawable.addtion );
}
} );
}
}
--------------------------------------------------------------------------------
res/layout/list.xml
--------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 这里是简单的ListView -->
<ListView android:id="@+id/ListView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
--------------------------------------------------------------------------------
res/layout/listitem.xml
<TextView android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="100px"
xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>
res/values/styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="theme" parent="android:Theme">
<!--设置ListView到整个Theme中-->
<item name="android:listViewStyle">@style/listS</item>
</style>
<!--配置ListView 的 Theme-->
<style name="listS" parent="android:style/Widget.AbsListView">
<item name="android:listSelector">@drawable/icon</item>
</style>
</resources>
--------------------------------------------------------------------------------
图片放进去有点丑陋,这里仅仅做为演示(演示结果为我写的另一个例子)。
ListView背景:
[img]http://dl.iteye.com/upload/attachment/351367/bf716d98-c7ce-37a9-9a38-a7a8cee97b7a.jpg[/img]
点击item设置的背景:
[img]http://dl.iteye.com/upload/attachment/351369/681928eb-bc49-3058-bc9f-0e5f27aa3814.jpg[/img]