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

您的位置:首頁技術文章
文章詳情頁

Android ListView UI組件使用說明

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

一、ListView

該組件是android中最常用的一個UI組件,用于實現在屏幕上顯示​多個內容,以便于我們用手指來回翻轉。

先在layout中進行布局我們的組件

<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>

對該組件注冊一個list_view的ID(這個R中的語句是運行時會自動生成的),可在這里看到

Android ListView UI組件使用說明

這樣這個組件就定義好了,然后在活動的源碼中進行注冊

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); }}

可以看出這里使用了一個Android自帶適配器類ArrayAdapter,使用泛型String的實例創建,然后傳入參數,分別為上下文實例,android自帶的一個list_item_1的內部布局文件,里面只有一個TextView,可用于顯示一段簡單的文本;最后一個參數就是我們傳入的數據​。

創建一個ListView的實例,并且找到這個​R文件的listView地址。最后調用setAdapter()方法,即為設置完畢。

Android ListView UI組件使用說明

二、源碼:

項目地址

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 克东县| 岳阳县| 友谊县| 隆德县| 普格县| 明星| 沙雅县| 洪洞县| 庆阳市| 清水河县| 玉田县| 栾城县| 乐至县| 新昌县| 阳信县| 丰镇市| 荔浦县| 洛川县| 龙海市| 宝兴县| 阿拉尔市| 鲁甸县| 萨迦县| 灵丘县| 沧州市| 通许县| 平湖市| 阳高县| 永州市| 石棉县| 龙游县| 中山市| 修水县| 临猗县| 泸定县| 景宁| 安阳县| 英山县| 贡觉县| 运城市| 锦州市|