xml代碼
listview_layout.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" >
? ? <!-- 直接使用數(shù)組資源給出列表項(xiàng),設(shè)置使用紅色分割條 -->
? ? <!--
? ? <ListView
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:entries="@array/love"
? ? android:divider="#f00"
? ? android:dividerHeight="2px"
? ? android:headerDividersEnabled="false"></ListView>
? ? -->
? ? <!-- 使用紅色分割條 -->
? ? <ListView
? ? ? ? android:id="@+id/list1"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:divider="#f00"
? ? ? ? android:dividerHeight="2px"
? ? ? ? android:headerDividersEnabled="false" />
? ? <!-- 使用綠色分隔條 -->
? ? <ListView
? ? ? ? android:id="@+id/list2"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:divider="#0f0"
? ? ? ? android:dividerHeight="2px"
? ? ? ? android:headerDividersEnabled="false" >
? ? </ListView>
</LinearLayout>
***array_item.xml***
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:id="@+id/TextView"
? ? android:textSize="24dp"
? ? android:padding="10px"
? ? android:shadowColor="#f0f"
? ? android:shadowDx="4"
? ? android:shadowDy="4"
? ? android:shadowRadius="2"
? ? >
</TextView>
***checked_item.xml***
<?xml version="1.0" encoding="utf-8"?>
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="wrap_content"
? ? android:shadowColor="#00f" >
</CheckBox>
JAVA實(shí)現(xiàn)代碼
package com.example.lovemia;
import android.R.string;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.os.Build;
public class MiaActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_mia);
setContentView(R.layout.listview_layout);
ListView li = (ListView) findViewById(R.id.list1);
// 定義一個(gè)數(shù)組
String[] str1 = new String[] { "mia", "love mia", "princess", "my love" };
// 將數(shù)組包裝為ArrayAdapter
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
R.layout.array_item, str1);
// 為L(zhǎng)istView設(shè)置Adapter
li.setAdapter(aa);
ListView li2 = (ListView) findViewById(R.id.list2);
// 定義數(shù)組
String[] str2 = new String[] { "010802", "love", "mia", "miss" };
// 將數(shù)組包裝為ArrayAdapter
ArrayAdapter<String> aa2 = new ArrayAdapter<String>(this,
R.layout.checked_item, str2);
// 為listView設(shè)置Adapter
li2.setAdapter(aa2);
}
}
界面效果

// 基于ListActivity實(shí)現(xiàn)列表 不使用xml文件
public class MiaActivity extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// 注意這個(gè)是在只有一個(gè)listview的情況下可以使用的
//無(wú)需使用布局文件
String[] str3=new String[]{
"mia","miss mia","love mia","010802"
};
//創(chuàng)建ArrayAdapter對(duì)象
ArrayAdapter<String> aa3=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice,str3);
//設(shè)置窗口顯示列表
setListAdapter(aa3);
}
}
界面效果
