欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android ListView UI組件使用說明

 更新時間:2020年04月15日 11:02:11   作者:心悅君兮君不知-睿  
這篇文章主要介紹了Android ListView UI組件使用說明,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

一、ListView

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

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  </ListView>
</LinearLayout>

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

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

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的實例創(chuàng)建,然后傳入?yún)?shù),分別為上下文實例,android自帶的一個list_item_1的內(nèi)部布局文件,里面只有一個TextView,可用于顯示一段簡單的文本;最后一個參數(shù)就是我們傳入的數(shù)據(jù)​。

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

二、源碼:

項目地址

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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論