Android ListView UI組件使用說(shuō)明
一、ListView
該組件是android中最常用的一個(gè)UI組件,用于實(shí)現(xiàn)在屏幕上顯示多個(gè)內(nèi)容,以便于我們用手指來(lái)回翻轉(zhuǎn)。
先在layout中進(jìn)行布局我們的組件
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='match_parent' > <ListView android: android:layout_width='match_parent' android:layout_height='match_parent' > </ListView></LinearLayout>
對(duì)該組件注冊(cè)一個(gè)list_view的ID(這個(gè)R中的語(yǔ)句是運(yùn)行時(shí)會(huì)自動(dòng)生成的),可在這里看到
這樣這個(gè)組件就定義好了,然后在活動(dòng)的源碼中進(jìn)行注冊(cè)
package com.example.listviewtest;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.widget.ArrayAdapter;import android.widget.ListView;//import java.lang.ArrayAdapter;public class MainActivity extends Activity { private String[] data = {'Apple','Banana','Orange','Watermelon','Pear','Grape','Pineapple','Strawberry','Cherry'}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,data); ListView listView = (ListView) findViewById(R.id.list_view); listView.setAdapter(adapter); }}
可以看出這里使用了一個(gè)Android自帶適配器類ArrayAdapter,使用泛型String的實(shí)例創(chuàng)建,然后傳入?yún)?shù),分別為上下文實(shí)例,android自帶的一個(gè)list_item_1的內(nèi)部布局文件,里面只有一個(gè)TextView,可用于顯示一段簡(jiǎn)單的文本;最后一個(gè)參數(shù)就是我們傳入的數(shù)據(jù)。
創(chuàng)建一個(gè)ListView的實(shí)例,并且找到這個(gè)R文件的listView地址。最后調(diào)用setAdapter()方法,即為設(shè)置完畢。
二、源碼:
項(xiàng)目地址
https://github.com/ruigege66/Android/tree/master/ListViewTest
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 得到XML文檔大小的方法2. WMLScript的語(yǔ)法基礎(chǔ)3. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法4. ASP中if語(yǔ)句、select 、while循環(huán)的使用方法5. xml中的空格之完全解說(shuō)6. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……7. 輕松學(xué)習(xí)XML教程8. html小技巧之td,div標(biāo)簽里內(nèi)容不換行9. XML入門的常見問(wèn)題(四)10. msxml3.dll 錯(cuò)誤 800c0019 系統(tǒng)錯(cuò)誤:-2146697191解決方法
