|
总体说来就是要在List的Item处于各个状态下任意进行背景图片的设置。
列出这个项目的建立过程和相关代码:
首先在res/drawable目录下放入一张适当的背景图片,我这里是now3.png,还是那张橘黄色的图片。反正是自定义的某个图片就行。
在res/drawable目录下建立jbshapp.xml文件。设置列表的页眉和页脚的背景样式。
java代码: <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- shape可选值 rectangle,oval,line, ring -->
<gradient android:startColor="#3690fa"
android:centerColor="#ffffff"
android:endColor="#509245"
android:type="linear"
android:angle="90"
android:centerX="0.5"
android:centerY="0.5" />
<!-- angle 为90代表代表上下渐变(从下往下开始) 0代表左右渐变
type 可选值 radial(径向) 和sweep和linear-->
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius="4dp" />
</shape>
然后同样在res/drawable目录下新建文件list_bg.xml,这里主要用于定义列表某一项处于什么状态对应什么背景图片。代码如下:
java代码: <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/now3" />
<!--这里就是解决问题的关键-->
<item android:state_selected="true"
android:drawable="@drawable/now3" />
</selector>
除了具体指定具体添加的背景图片外,还可以在item里面嵌套shape。比如可写成:
java代码: <?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/now3" />
<item android:state_selected="true">
<shape>
<gradient android:startColor="#8600ff" />
<stroke android:width="2dp"
android:color="#000000" />
<corners android:radius="5dp" />
<padding android:left="10dp"
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
</shape>
</item>
</selector>
接着是res/layout目录下的布局文件。首先是整体大的布局文件main.xml:
java代码: <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout android:id="@+id/listHeader"
android:background="@drawable/jbshapp"
android:layout_alignParentTop="true"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="web前端技术"
android:textColor="#000000"
android:textSize="18dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
<RelativeLayout android:id="@+id/listFooter"
android:background="@drawable/jbshapp"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button android:id="@+id/prePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示选择内容"
android:layout_alignParentLeft="true"></Button>
</RelativeLayout>
<ListView android:id="@+id/myListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/listHeader"
android:layout_above="@id/listFooter">
</ListView>
</RelativeLayout>
这里用相对布局定义了页眉和页脚。利用android:background="@drawable/jbshapp"这个属性就能设置定义好的背景样式。然后是列表某一项的布局文件listitem.xml文件,代码如下:
java代码: <?xml version="1.0" encoding="utf-8"?>
<TextView android:id="@+id/TextView01"
android:background="@drawable/list_bg"
android:layout_width="fill_parent"
android:gravity="center_vertical"
android:textSize="20dip"
android:layout_height="60px"
xmlns:android="http://schemas.android.com/apk/res/android">
</TextView>
里面只有一个TextView,利用android:background="@drawable/list_bg"把定义好的selector作为背景。
ActivityMain.java代码:
java代码: public class ActivityMain extends Activity {
Button bt;
String result="$";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView(R.layout.main);
final ListView lv = (ListView)findViewById(R.id.myListView);
String[] data=new String[]{"JQTouch","iUI","YUI","Dojo","Sencha Touch","Ext Js","Kissy","JQuery Mobile"};
ArrayAdapter<String> ss = new ArrayAdapter<String>(this,R.layout.listitem,data);
lv.setAdapter( ss );
lv.setOnItemClickListener( new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
// setTitle(arg0.getItemAtPosition(arg2).toString());
arg1.setSelected(true); //让这个view处于被选中状态
result=arg0.getItemAtPosition(arg2).toString(); } );
lv.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
result=arg0.getItemAtPosition(arg2).toString(); @Override
public void onNothingSelected(AdapterView<?> arg0) {} );
bt=(Button)findViewById(R.id.prePage);
bt.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(result.equals("$"))setTitle("请选择");
else setTitle(result); } }
}
|
|