標籤:android
ListView簡介
ListView是android中常用的一種控制項,建立ListView有兩種方式:
1.在xml中使用ListView控制項建立。
2.使用activity繼承ListActivity,然後使用setListAdapter()建立,如果需要在xml中建立,需要android:id = @id:android:list
Listview和GridView和Spinner和Gallery等Adapterview都是容器,用adapter來提供資料,而adapterView負責資料的顯示形式。
AdapterView屬性講解
AbsListView的屬性:
ListView提供的屬性
在xml設定ListView執行個體
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"><ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:entries="@array/books" android:divider="#f00" android:dividerHeight="2px" android:headerDividersEnabled="false" /></LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <resources> <string-array name="books"> <item>hello</item> <item>world</item> <item>liu</item> <item>peng</item> </string-array> </resources>
使用ArrayAdapter建立ListView
package peng.liu.testview;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.os.SystemClock;import android.view.MotionEvent;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.Chronometer;import android.widget.ImageView;import android.widget.ListView;public class MainActivity extends Activity{ private String attrs[] = {"hello","world","liu","peng‘"}; private String names[] = {"java","c","python"}; private ListView list1,list2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list1 = (ListView) findViewById(R.id.list1); list2 = (ListView) findViewById(R.id.list2); ArrayAdapter<String> array1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,attrs); ArrayAdapter<String> array2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,names); list1.setAdapter(array1); list2.setAdapter(array2); }}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:orientation="vertical"> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list1" android:dividerHeight="2px" android:divider="#f00" android:headerDividersEnabled="false" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/list2" android:dividerHeight="2px" android:divider="#f00" android:headerDividersEnabled="false" /></LinearLayout>
基於ListActivity建立
package peng.liu.testview;import android.app.ListActivity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class MainActivity extends ListActivity { private String attrs[] = {"hello","world","liu","peng‘"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> array1 = new ArrayAdapter(this,android.R.layout.simple_list_item_1,attrs); setListAdapter(array1); }}
Android列表視圖ListView和ListActivity-android學習之旅(二十四)