標籤:大全 bdc 入門 ogg item 使用方法 處理 xmlns his
相信通過前兩期的學習,以及會開發最簡單的一些列表介面了吧,那麼本期接著來學習更多方法技巧。
一、使用ListActivity
如果程式的視窗僅僅需要顯示一個列表,則可以直接讓Activity繼承ListActivity來實現, ListActivity的子類無須調用setContentView()方法來顯示某個介面,而是可以直接傳入一個內容Adapter,ListActivity的子類就呈現出一個列表。
接下來通過一個簡單的樣本程式來學習基於ListActivity實現列表。
繼續使用WidgetSample工程的listviewsample模組,在java包下建立MyListActivity.java檔案,具體代碼如下:
package com.jinyu.cqkxzsxy.android.listviewsample;import android.app.ListActivity;import android.os.Bundle;import android.widget.ArrayAdapter;public class MyListActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 定義一個數組 String[] persons = {"蠟筆小新", "皮卡丘", "蜘蛛俠", "灰太狼", "黑貓警長", "孫悟空", "忍者神龜", "米老鼠", "HelloKitty", "櫻桃小丸子"}; // 將數組封裝成ArrayAdapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, persons); // 設定該視窗顯示列表 setListAdapter(adapter); }}
ListActivity的布局檔案中只有一個ListView,只需要為ListActivity設定Adapter即可。
運行程式,可以看到所示介面效果。
從可以看到,ListActivity的預設布局是由一個位於螢幕中心的列表組成的。
二、自訂欄表項
前面學習ListView都是使用的Android系統自訂欄表項資源,基本都是一些純文字的資源,介面不夠炫目,也沒有辦法定製。在實際開發中,列表經常包括表徵圖、按鈕等組件,這就需要開發人員自訂欄表項來完成了。關鍵是需要給適配器Adapter提供足夠的資料,讓Adapter能夠用更豐富的View對象來填充列表的每一行。
接下來就通過一個樣本來學習如何自訂欄表項。
同樣使用WidgetSample工程的listviewsample模組,在app/main/res/layout/目錄下建立custom_item_layout.xml檔案,在其中填充如下程式碼片段:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="wrap_content" /></LinearLayout>
在res/layout/目錄下建立一個custom_item.xml的清單項目布局檔案,其代碼如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:gravity="center_vertical"> <ImageView android:id="@+id/icon_img" android:layout_width="50dp" android:layout_height="50dp" android:padding="5dp" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/content_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:textColor="#00f"/></LinearLayout>
這個布局使用LinearLayout作為一行,其中表徵圖位於左側,文本位於右側。
接下來為ListView提供Adapter,Adapter決定了ListView所要顯示的清單項目。在java包下建立CustomItemActivity.java檔案,載入上面建立的布局檔案,具體代碼如下:
package com.jinyu.cqkxzsxy.android.listviewsample;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class CustomItemActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_item_layout); // 擷取介面ListView組件 ListView listView = (ListView) findViewById(R.id.listview); // 定義一個List集合 final List<String> components = new ArrayList<>(); components.add("TextView"); components.add("EditText"); components.add("Button"); components.add("CheckBox"); components.add("RadioButton"); components.add("ToggleButton"); components.add("ImageView"); // 將List封裝成ArrayAdapter ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_item, R.id.content_tv, components); // 為ListView設定Adapter listView.setAdapter(adapter); // 為ListView清單項目綁定點擊事件監聽器 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { Toast.makeText(CustomItemActivity.this, components.get(position), Toast.LENGTH_SHORT).show(); } }); }}
以上代碼遵循前面學習的ListView樣本的整體結構。其主要的區別就是使用了自訂欄表布局R.layout.list_item。建立ArrayAdapter必須指定如下四個參數。
context:要使用的上下文環境,幾乎建立所有組件都需要傳入Context對象。
resource: 要使用的自訂欄表項布局資源 ID。
textViewResourceId:自訂欄表布局中TextView的ID,該TextView組件將作為ArrayAdapter的清單項目組件。
objects:要實際顯示的數組或List,將負責為多個清單項目提供資料。 該數組或List包含多少個元素,就將產生多少個清單項目。
運行程式,可以看到所示介面效果。
從可以看到,列表布局裡面使用了我們自訂的表徵圖,也修改了文本顯示樣式。
但是在這個樣本中,所有的表徵圖都是相同的,往往不能滿足實際開發需求,會在下一節中來進行學習。
今天就先到這裡,如果有問題歡迎留言一起探討,也歡迎加入Android零基礎入門技術討論群,共同成長!
此文章著作權為公眾號分享達人秀(ShareExpert)——鑫鱻所有,若需轉載請聯絡作者授權,特此聲明!
往期總結分享:
Android零基礎入門第1節:Android的前世今生
Android零基礎入門第2節:Android 系統架構和應用組件那些事
Android零基礎入門第3節:帶你一起來聊一聊Android開發環境
Android零基礎入門第4節:正確安裝和配置JDK, 高富帥養成第一招
Android零基礎入門第5節:善用ADT Bundle, 輕鬆邂逅女神
Android零基礎入門第6節:配置最佳化SDK Manager, 正式約會女神
Android零基礎入門第7節:搞定Android模擬器,開啟甜蜜之旅
Android零基礎入門第8節:HelloWorld,我的第一趟旅程出發點
Android零基礎入門第9節:Android應用實戰,不懂代碼也可以開發
Android零基礎入門第10節:開發IDE大升級,終於迎來了Android Studio
Android零基礎入門第11節:簡單幾步帶你飛,運行Android Studio工程
Android零基礎入門第12節:熟悉Android Studio介面,開始裝逼賣萌
Android零基礎入門第13節:Android Studio配置最佳化,打造開發利器
Android零基礎入門第14節:使用高速Genymotion,跨入火箭時代
Android零基礎入門第15節:掌握Android Studio項目結構,揚帆起航
Android零基礎入門第16節:Android使用者介面開發概述
Android零基礎入門第17節:TextView屬性和方法大全
Android零基礎入門第18節:EditText的屬性和使用方法
Android零基礎入門第19節:Button使用詳解
Android零基礎入門第20節:CheckBox和RadioButton使用大全
Android零基礎入門第21節:ToggleButton和Switch使用大全
Android零基礎入門第22節:ImageView的屬性和方法大全
Android零基礎入門第23節:ImageButton和ZoomButton使用大全
Android零基礎入門第24節:自訂View簡單使用,打造屬於你的控制項
Android零基礎入門第25節:簡單且最常用的LinearLayout線性布局
Android零基礎入門第26節:兩種對齊,layout_gravity和gravity大不同
Android零基礎入門第27節:正確使用padding和margin
Android零基礎入門第28節:輕鬆掌握RelativeLayout相對布局
Android零基礎入門第29節:善用TableLayout表格版面配置
Android零基礎入門第30節:兩分鐘掌握FrameLayout幀布局
Android零基礎入門第31節:少用的AbsoluteLayout絕對布局
Android零基礎入門第32節:新推出的GridLayout網格布局
Android零基礎入門第33節:Android事件處理概述
Android零基礎入門第34節:Android中基於監聽的事件處理
Android零基礎入門第35節:Android中基於回調的事件處理
Android零基礎入門第36節:Android系統事件的處理
Android零基礎入門第37節:初識ListView
Android零基礎入門第38節:初識Adapter
Android零基礎入門第39節:ListActivity和自訂欄表項