Android列表视图ListView和ListActivity-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的属性:

Android列表视图ListView和ListActivity-android学习之旅(二十四)

Android列表视图ListView和ListActivity-android学习之旅(二十四)

ListView提供的属性

Android列表视图ListView和ListActivity-android学习之旅(二十四)

在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);
    }

}
上一篇:ALTER TABLE


下一篇:CSS3学习(圆角、图片、阴影、背景、渐变、文本、字体、2D、3D、过渡等)