Android列表視圖ListView和ListActivity-android學習之旅(二十四)

來源:互聯網
上載者:User

標籤: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學習之旅(二十四)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.