安卓学习第10课——listview

1.普通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:orientation="vertical">
<ListView 
     android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:entries="@array/books"
    android:divider="#f00"
    android:dividerHeight="2sp"
    android:background="#6E4A90"
    android:headerDividersEnabled="false"
    />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string-array name="books">
           <item >疯狂英语1</item> 
         <item >疯狂英语2</item> 
          <item >疯狂英语3</item> 
           <item >疯狂英语4</item> 
        <item >疯狂英语5</item> 
        </string-array>
</resources>

可以用xml创建数组。

2.用ArrayAdapter创建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:orientation="vertical">

    <ListView
        android:id="@+id/list1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#f00"
        android:dividerHeight="2sp"
        android:headerDividersEnabled="false" />
 <ListView
        android:id="@+id/list2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#0f0"
        android:dividerHeight="2sp"
        android:headerDividersEnabled="false" />
        

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:textSize="24sp"
    android:padding="10sp"
    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:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:textSize="24sp"
    android:padding="10sp"
    android:checkMark="@drawable/ok"
    android:shadowColor="#f0f"
    android:shadowDx="4"
    android:shadowDy="4"
    android:shadowRadius="2" />
 
package com.example.arrayadapter;

import android.app.Activity;
import android.os.Bundle;
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 list1=(ListView) findViewById(R.id.list1);
        String[] arr1={"孙悟空","猪八戒","牛魔王"};
        ArrayAdapter<String> adapter1=new ArrayAdapter<String>(this,R.layout.array_item,arr1);
        list1.setAdapter(adapter1);
        ListView list2=(ListView) findViewById(R.id.list2);
        String[] arr2={"java","spring","android"};
        ArrayAdapter<String> adapter2=new ArrayAdapter<String>(this,R.layout.checked_item,arr2);
        list2.setAdapter(adapter2);
    }

}

注》疑问,checkMark用法

安卓学习第10课——listview,布布扣,bubuko.com

安卓学习第10课——listview

上一篇:IOS7 隐藏状态栏 (电池栏)


下一篇:Android 2.3发短信详细流程