国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Android ListView UI組件使用說(shuō)明

瀏覽:2日期:2022-09-24 17:47:13

一、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)生成的),可在這里看到

Android ListView UI組件使用說(shuō)明

這樣這個(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è)置完畢。

Android ListView UI組件使用說(shuō)明

二、源碼:

項(xiàng)目地址

https://github.com/ruigege66/Android/tree/master/ListViewTest

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 武宣县| 秀山| 朝阳区| 长宁县| 眉山市| 宜兴市| 乳源| 和田市| 萨嘎县| 舞阳县| 安徽省| 阜宁县| 濮阳市| 交城县| 东乡| 鱼台县| 武陟县| 若尔盖县| 南岸区| 双江| 巨野县| 龙海市| 宜昌市| 灵台县| 青河县| 运城市| 莱阳市| 开阳县| 新乡市| 延寿县| 云龙县| 柳林县| 楚雄市| 东丽区| 宝兴县| 易门县| 葵青区| 诸城市| 鄯善县| 鄂尔多斯市| 临澧县|