編輯:關於 Android 程式設計
我們在使用一些 App 時,應該很少看到完全不加修飾的「裸控件」吧?除非是系統內建軟體,為了風格統一而做出權衡。這裡並不是要批評 Android 原生控件不好看——說實話,我很喜歡 Android 的一些原生控件,只是為了整體風格的一致性,偶爾還是得在美工上多花點心思。就這點而言,我對某訊的產品設計頗為欣賞。接下來,就讓我們一步步走進 Android UI 自定義控件的世界。
自定義控件就像堆積木,我們挑選積木、為它上色、再賦予它功能說明。下面用一個小例子來逐一討論。
本示例選用 ImageButton 做一個簡單分析,先看看運行效果:
搭雛形時,第一步是挑選積木。我們選一個 ImageView 和一個 TextView,上下擺放,再用適當的間距把兩者綁在一起:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:text="確定"
android:textColor="#000000" />
</LinearLayout>
這段程式碼只能得到一個居中的方圖、下方的文字,以及貼合兩者邊緣的間距。
接下來我們要為它分配顏色、賦予功能,這些都實現在 Java 程式碼中。關鍵程式碼如下:
/** 設置圖片資源 */
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/** 設置顯示的文字 */
public void setTextViewText(String text) {
textView.setText(text);
}
不過僅僅讓這個「Button」更好看還不夠,我們還要做另一件事:例如點擊後要執行一些綁定的、使用該控件時必然會進行的操作。這與「加外套」是同一個道理:
@Override
public void setOnClickListener(OnClickListener l) {
auxiliaryFunction();
super.setOnClickListener(l);
}
protected void auxiliaryFunction() {
Log.i(TAG, "log message.");
}
這個額外功能是否真的執行,可以在 Log 日誌中查看確認。
既然是自定義,這個 ImageButton 的原始父類別自然不是 Button。真相如下:
public class ImageButton extends LinearLayout {
private ImageView imageView;
private TextView textView;
public ImageButton(Context context) {
super(context);
}
public ImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.image_button, this);
imageView = findViewById(R.id.imageView1);
textView = findViewById(R.id. textView1);
}
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
public void setTextViewText(String text) {
textView.setText(text);
}
@Override
public void setOnClickListener(OnClickListener l) {
auxiliaryFunction();
super.setOnClickListener(l);
}
protected void auxiliaryFunction() {
Log.i(TAG, "log message.");
}
}
這裡有一點要特別注意:必須指明自定義控件的完整類別路徑,如下:
<com.demo.customview.imagebutton.widgets.ImageButton
android:id="@+id/btn_right"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="@drawable/custom_button" />
Button 的動作不外乎觸摸、按下與抬起三種,這三種效果的配置需要在 res 下的 drawable 資料夾中建立(若無此資料夾請新建一個)selector:
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_unpress_background"
android:state_focused="true" android:state_pressed="false" />
<item android:drawable="@drawable/button_pressed_background"
android:state_pressed="true" />
<item android:drawable="@drawable/button_unpress_background"
android:state_focused="false" android:state_pressed="false" />
</selector>
在 Java 程式碼中的用法與 Button 幾乎無異:
public class MainActivity extends Activity {
private ImageButton mImageBtn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageBtn1 = findViewById(R.id.btn_right);
mImageBtn1.setTextViewText("確定");
mImageBtn1.setImageResource(R.drawable.right_icon);
mImageBtn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "點擊確定", 0).show();
}
});
}
}
從挑積木、上色,到賦予功能與觸摸回饋,一個專屬的 ImageButton 就這樣完成了。掌握這套思路後,你也能打造各種符合自身設計風格的自定義控件。
自定義 PathTextView:實作可點擊層級的簡易路徑導航條
檔案管理員頂部那種「本機 > 文件 > 專案 > src」的路徑列,是層級式介面裡非常實用的小元件。本文把它封裝成一個可直接複用的自定義 View。
Launcher3 壁紙機制解析:從選擇、裁切到動態壁紙
壁紙是系統能力 Launcher 的壁紙設定並非 Launcher 獨有,而是透過系統 WallpaperManager 完成。因此,不只是 Launcher,許多第三方
Android 學習筆記:自訂樣式 Toast 提示框
系統預設的 Toast 只能顯示一行純文字,樣子單調。如果想放圖示、換底色或做更複雜的排版,就得自己指定版面。以下示範完整做法。 1. 準備 Toast 專用的版面檔
Android 5.0 Settings 原始碼簡要分析
概述 近來有機會接觸 Android 原始碼,趁此機會記錄一些收穫。本文針對 L(5.0)平台上 Settings 模組的正常啟動流程做一個簡要分析,並嘗試解析 Sett