編輯:Android 開發實例
本文探討組合 View(Layout)使用不當時出現的怪異現象,下一篇再分析成因與解法。
開發 Android App 時經常需要「一個圖示 + 兩行文字」這種組合控制項,用寫法如下:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:id="@+id/ll1"
android:background="@android:drawable/btn_default"
android:focusable="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/feedicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Line one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="Line two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
要掛點擊事件時,程式碼是:
LinearLayout ll1 = (LinearLayout) findViewById(R.id.ll1); ll1.setOnClickListener(listener);
在一個頁面裡放多個這種組合 View,並為每個都設上 android:clickable="true" 與 android:focusable="true":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/ll"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/ll1"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:drawable/btn_default" >
<ImageView
android:src="@drawable/feedicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="Line one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="Line two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<!-- ll2 結構與 ll1 相同 -->
</LinearLayout>
</ScrollView>
</LinearLayout>
透過指定 LinearLayout 的 clickable(若在程式碼中設定了 OnClickListener,系統會自動幫你打開這個屬性)與 focusable,每個組合 View 都能用軌跡球或方向鍵取得焦點,點擊時也會顯示焦點狀態。這種用法是正常的。
但在某些情況下,組合 View 顯示的內容(圖示、上下兩行文字)是依其他資料模型動態決定的。為了簡化程式碼,我們很自然會想寫一個自訂 View 把這段結構封裝起來。
版面配置 icon_text_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@android:drawable/btn_default"
android:focusable="true"
android:clickable="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/feedicon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="IconTextView Line one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:text="IconTextView Line two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
自訂的 IconTextView 類別:
package org.goodev.viewtest;
public class IconTextView extends LinearLayout {
ImageView mImageView;
TextView mTextOne;
TextView mTextTwo;
public IconTextView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.icon_text_view, this, true);
// 這裡接著找出 mImageView、mTextOne、mTextTwo
}
public void setData(Drawable icon, String line1, String line2) {
mImageView.setImageDrawable(icon);
mTextOne.setText(line1);
mTextTwo.setText(line2);
}
}
動態把它加入 Layout:
LinearLayout ll = (LinearLayout) findViewById(R.id.ll); IconTextView itv = new IconTextView(this); itv.setOnClickListener(listener); ll.addView(itv, 0);
此時你會發現:IconTextView 在按下時沒有顯示對應的狀態(背景圖不會換成 Press 樣式)。
若把 icon_text_view.xml 中的 clickable 改為 true,按下時的背景就正常了——但接著又冒出新的問題:IconTextView 的點擊事件沒了,使用者點擊這個組合 View 完全不會觸發 OnClickListener。
於是形成一個尷尬的局面:直接寫在版面配置中的組合 View 可以同時正常顯示 Press 狀態並響應點擊,一旦改用 IconTextView 封裝,兩者卻變成不可兼得。為什麼會這樣?下一篇我們從 View 階層結構的角度拆解這個問題。
Android 本地化實作:從 values-it 到語言碼對照表的完整整理
Android 應用程式可能在許多不同地區的許多裝置上執行。為了讓應用程式更具互動性、也更貼近使用者,App 應該能處理符合當地語言環境的文字、數字與檔案格式。本章要解釋
Android 個人理財實作之一:專案架構與啟動畫面的實現
從這一篇開始,我們要一起動手做一個 Android 個人理財工具,並把整个開發歷程記錄下來,與大家分享。既然是系列的第一篇,先把全局藍圖交代清楚,再動手做第一塊拼圖——啟
Android 下拉刷新完全解析:一分鐘實作下拉刷新功能(附源碼)
最近專案中需要用到 ListView 的下拉刷新功能。起初想省事,直接從網上找現成的方案,但試了多個版本後發現效果都不理想——有些功能不完整或有 Bug,有些使用起來太過
Android MediaPlayer 完整指南:播放、暫停與快轉音訊
Android 提供 MediaPlayer 類別,可播放音訊與影片。若只是播放應用內建的短音樂,最簡單的方式是使用 MediaPlayer.create。 MediaP