@version:Android4.3 API18
@author:liuxinming
XML属性 | 相关方法 | 说明 |
android:choiceMode | 设置AbsListView的选择行为,支持下列属性值 none:不显示任何选中项 value=0 singleChoice:允许单选 value=1 multipleChoice:允许多选 value=2 multipleChoiceModal:允许多选 value=3 |
|
android:DrawSelectorOnTop | setDrawSelectoronTop(boolean) | true:选中的列项将会显示在上面 |
android:fastScrollEnabled | 设置是否允许快速滚动 true:显示滚动图标,并允许用户拖动该滚动图标进行快速滚动 |
|
android:listSelector | setSelector(int) | 指定被选中的列表项上绘制的Drawable |
android:scrollingCache | true:该组件在滚动时将会使用绘制缓存 | |
android:smoothScrollbar | setSmoothScrollbarEnable(boolean) | false:则不在header View之后绘制分割线 |
android:stackFromBottom | 设置是否从底端开始排列列表项 | |
android:textFilterEnable | 设置是否对列表项进行过滤 注释:当该AbsListView对应的Adapter实现了Filter接口时该属性才会起作用 |
|
android:transcriptMode | 设置该组件的滚动模式。该属性支持如下属性 disabled:关闭滚动。默认值 normal:当该AbsListView受到数据改变通知,且最后一个列表项可见时,该AbsListView将会滚动到底端 alwaysScroll:该AbsListView总会自动滚动到底端 |
android:divider | 设置List列表项的分割线(即可用颜色分割,也可用Drawable分割) |
android:dividerHeight | 分割线的高度 |
android:entries | 指定一个数组资源,Android将根据该数组资源来生成ListView |
android:footerDividersEnabled | false:则不在footer View之前绘制分隔线 |
android:headerDividersEnabled | false:则不在header View之后绘制分隔线 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 使用数组资源给出列表项 --> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/code" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <resources> <string-array name="code"> <item>Android</item> <item>PHP</item> <item>Java</item> <item>C</item> <item>C++</item> </string-array> </resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <!-- 定义两个ListView --> <ListView android:id="@+id/list01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> <ListView android:id="@+id/list02" android:layout_width="fill_parent" android:layout_height="wrap_content" android:divider="#f00" android:dividerHeight="2dp" android:headerDividersEnabled="false" /> </LinearLayout>Android_ListView/res/layout/array_item.xml
<?xml version="1.0" encoding="UTF-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:padding="10dp" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2" />
<?xml version="1.0" encoding="utf-8"?> <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/TextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:checkMark="@drawable/ok" android:shadowColor="#f0f" android:shadowDx="4" android:shadowDy="4" android:shadowRadius="2"/>
package com.example.android_listview; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ArrayAdapter; import android.widget.ListView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ListView list01 = (ListView) findViewById(R.id.list01); ListView list02 = (ListView) findViewById(R.id.list02); //定义数组 String[] data1 = {"屌丝","高富帅","白富美"}; String[] data2 = {"Android","PHP","Java","C","C++"}; //将数组包装为ArrayAdapter ArrayAdapter<String> adapter1 = new ArrayAdapter<String> (this,R.layout.array_item,data1); ArrayAdapter<String> adapter2 = new ArrayAdapter<String> (this,R.layout.checked_item,data2); //为ListView 设置 Adapter list01.setAdapter(adapter1); list02.setAdapter(adapter2); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
package com.example.android_listview; import android.app.ListActivity; import android.os.Bundle; import android.widget.ArrayAdapter; /** * @author LiuxinMing [email protected] */ public class MainActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 无需使用布局文件 String[] arr = {"屌丝","高富帅","白富美"}; // 创建ArrayAdapter对象 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, arr); // 设置该窗口显示列表 setListAdapter(adapter); } }