1.第一种是使用listview创建列表视图,android:divider属性用于设置分隔线,android:dividerHetght属性设置分隔线高度,android:footDividersEnabled属性用于是否在footer view之前绘制分隔线,android:headerDividerEnabled属性用于是否在header view之后绘制分隔线
布局代码:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="3px"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:id="@+id/listview1"/>
创建用于指定列表项的数组资源文件,将其保存在values/arrays目录中
代码如下:
<resources>
<string-array name="ctype">
<item>情景模式</item>
<item>主题模式</item>
<item>手机</item>
<item>程序管理</item>
</string-array>
创建适配器:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource
(this, R.array.ctype, android.R.layout.simple_list_item_checked);
listView.setAdapter(adapter);
添加监听事件:
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos, long id) {
String result = (String) parent.getItemAtPosition(pos).toString();
Toast.makeText(ShituActivity.this, result, Toast.LENGTH_SHORT).show();
}
});
![](http://dl2.iteye.com/upload/attachment/0110/9472/e9f387d0-d9a2-3eb0-bb09-9a965a6ed879.jpg)
2.第二种方法是让Activity继承ListActivity来实现
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
在创建适配器之前新建一个String[]
创建适配器:
String[] ctype=new String[]{"情景模式","主题模式","手机","程序管理"};
ArrayAdapter<String> adapter =new ArrayAdapter<String>
(this,android.R.layout.simple_list_item_single_choice,ctype);
setListAdapter(adapter);
重写父类中的onListItemClick()方法:
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String result = l.getItemAtPosition(position).toString();
Toast.makeText(LihiActivity.this, result, Toast.LENGTH_SHORT).show();
}
![](http://dl2.iteye.com/upload/attachment/0110/9470/18732d5b-717b-3289-bf21-7d0d4761099e.jpg)