android开发基础_列表视图一(List View)
官網解釋:
? a view that shows items in a verically list,The items come from the ListAdapter associated with this view.
??創建listview有兩種方式:
????? 1.直接使用ListView進行創建(一:可以用數組,二:調用vlaues里面預先設置好的)
????? 2.讓activity集成listactivity
一旦程序中獲得了ListView之后,接下來就需要為ListView設置他要顯示的列表了, (需要提供一個顯示的列表項),就需要借助于內容Adapter了,內容Adapter負責提供需要顯示的列表項。
XML屬性
android:choiceMode???????? 設置listview的選擇行為
android:divider???????????????? 設置list列表項的分割條(既可以用顏色分割,也可以用Drawable分割)
android:dividerHeight??????? 設置分割條的高度
android:entries????????????? 制定一個數組資源,android根據數組資源生成ListView
android:footerDividersEnabled? 設置為false,則不再footer view之前繪制分隔條
android:headerDividersEnabled 如果設為false,則不再header view 后繪制分隔符
View Code 1 <?xml version="1.0" encoding="utf-8"?>2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <!-- 直接使用數組資源給出列表項 -->
13 <ListView
14 android:layout_width="fill_parent"
15 android:layout_height="wrap_content"
16 android:entries="@array/books"
17 android:divider="@drawable/red"
18 />
19 <!-- 使用Adapter提供列表項的ListView、 -->
20 <ListView
21 android:id="@+id/listview2"
22 android:layout_width="fill_parent"
23 android:layout_height="wrap_content"
24 android:divider="@drawable/green"
25 />
26 </LinearLayout>
java代碼如下:
?
View Code 1 package com.wbk.listview;2
3 import android.app.Activity;
4 import android.os.Bundle;
5 import android.widget.ArrayAdapter;
6 import android.widget.ListView;
7
8 public class ListviewActivity extends Activity {
9 /** Called when the activity is first created. */
10 @Override
11 public void onCreate(Bundle savedInstanceState) {
12 super.onCreate(savedInstanceState);
13 setContentView(R.layout.main);
14 ListView list2 = (ListView) findViewById(R.id.listview2);
15 // 定義一個數組
16 String[] arr = { "item1 ", "item2", "item3" };
17
18 // 將數組包裝arrayadapter
19
20 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
21 android.R.layout.simple_list_item_1, arr);
22 list2.setAdapter(arrayAdapter);
23 }
24 }
還有一個values如下:
View Code 1 <?xml version="1.0" encoding="UTF-8"?>2 <resources>
3 <string-array name="books">
4 <item>1</item>
5 <item>2</item>
6 <item>3</item>
7 <item>4</item>
8 </string-array>
9 </resources>
分析:以上采用了兩種方法實現了listview,
第一種是在values下新建一個xml文件,在xml文件中設置,listview設置所顯示的值,然后在布局文件下調用books
(android:entries="@array/books")
第二種方法是:在java里面findlistview,通過ArrayAdapter決定ListView顯示的組件,
運行程序結果如下:
?
??
?
?
轉載于:https://www.cnblogs.com/bokun-wang/archive/2011/09/28/2194607.html
總結
以上是生活随笔為你收集整理的android开发基础_列表视图一(List View)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: png的那点事儿
- 下一篇: HDU 1271整数对