在日常開發中,你有沒有自己動手做過自定義 UI 控件?今天花了些時間鑽研 Android 的自定義標題欄,把心得分享給大家。
設計良好的 App 幾乎都會自定義標題欄。多數人的做法是寫一個標題的 xml 布局,再通過 include 到各處引用——這比在每個布局文件裡重複寫一遍已經進步不少,但仍不是最優雅的方式。既然如此,我們為何不乾脆封裝一個標題欄控件呢?下面就用效果圖開場,帶大家一步步實現。
開始動手:
第一步:自定義 xml 屬性
新建一個 Android 項目,在 values 文件夾下創建 atts.xml,在其中聲明自定義控件需要暴露的屬性。
atts.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ToolBar">
<attr name="title" format="string" />
<attr name="titleTextSize" format="dimension" />
<attr name="titleTextColor" format="color" />
<attr name="leftBackground" format="reference|color" />
<attr name="leftText" format="string" />
<attr name="leftTextColor" format="reference|color" />
<attr name="rightBackground" format="reference|color" />
<attr name="rightText" format="string" />
<attr name="rightTextColor" format="reference|color" />
</declare-styleable>
</resources>
name 是我們要使用的屬性名,format 則表示該屬性接受的值的類型:string 為字符串,color 為顏色值,dimension 為尺寸,reference 表示可引用某個資源 id。其他常見的 format 還有 boolean(布爾值)、float(浮點數)、integer(整數)、fraction(百分比)、enum(枚舉)、flag(位或運算)。
第二步:自定義標題類
在 Java 中自定義一個類繼承 RelativeLayout 並重寫構造方法。我們的標題欄由三部分組成:左右各一個 Button,中間一個 TextView,因此這個類的核心工作就是處理這三個控件。
先聲明三個控件及相關參數,這些參數都來自 atts.xml 的設置。由於我們是從 xml 中引用自定義控件,屬性值都寫在 xml 裡,因此需要先把值取出來再賦給控件。
/** 標題欄的三個控件 */
private Button leftBtn, rightBtn;
private TextView title;
/** 左邊按鈕的屬性 */
private int leftTextColor;
private Drawable leftBackground;
private String leftText;
/** 右邊按鈕的屬性 */
private int rightTextColor;
private Drawable rightBackground;
private String rightText;
/** 中間 TextView 的屬性 */
private int titleTextColor;
private String titleText;
private float titleTextSize;
/** 三個控件的布局參數 */
private LayoutParams leftParams, rightParams, titleParams;
下面是構造方法:它接收上下文與 AttributeSet 兩個參數,後者是一組屬性集合,用來處理一批 xml 標籤。通過 ta.getXXX 從 xml 中取出屬性值,再逐一設置給控件,最後借助 LayoutParams 設定寬高並用 addView 添加到布局中。
public MyToolBar(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ToolBar);
leftTextColor = ta.getColor(R.styleable.ToolBar_leftTextColor, 0);
leftBackground = ta.getDrawable(R.styleable.ToolBar_leftBackground);
leftText = ta.getString(R.styleable.ToolBar_leftText);
rightTextColor = ta.getColor(R.styleable.ToolBar_rightTextColor, 0);
rightBackground = ta.getDrawable(R.styleable.ToolBar_rightBackground);
rightText = ta.getString(R.styleable.ToolBar_rightText);
titleText = ta.getString(R.styleable.ToolBar_title);
titleTextColor = ta.getColor(R.styleable.ToolBar_titleTextColor, 0);
titleTextSize = ta.getDimension(R.styleable.ToolBar_titleTextSize, 0);
// 對 ta 進行回收
ta.recycle();
leftBtn = new Button(context);
rightBtn = new Button(context);
title = new TextView(context);
/** 設置屬性 */
leftBtn.setText(leftText);
leftBtn.setTextColor(leftTextColor);
leftBtn.setBackground(leftBackground);
rightBtn.setText(rightText);
rightBtn.setTextColor(rightTextColor);
rightBtn.setBackground(rightBackground);
title.setText(titleText);
title.setTextColor(titleTextColor);
title.setTextSize(titleTextSize);
title.setGravity(Gravity.CENTER);
// 設置整體背景顏色
setBackgroundColor(0x7CFC0055);
leftParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
addView(leftBtn, leftParams);
rightParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
addView(rightBtn, rightParams);
titleParams = new LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT);
titleParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(title, titleParams);
}
第三步:引用自定義控件
控件寫好後,就可以在主布局中引用它。前三個以 android: 開頭的屬性是系統屬性,後面以 custombar 開頭的則是我們自定義的屬性。為了使用 custombar,需要在根布局中添加命名空間聲明:
xmlns:custombar="https://schemas.android.com/apk/res/com.example.mytoolbar"
注意末尾的 com.example.mytoolbar 要換成你應用的包名。如果你用的是 Android Studio 而非 Eclipse,就更省事,直接寫:
xmlns:custombar="https://schemas.android.com/apk/res-auto"
接著在布局中引用我們的控件,並配置各項自定義屬性:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:custombar="https://schemas.android.com/apk/res/com.example.mytoolbar"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.mytoolbar.MyToolBar
android:id="@+id/mytoolbar"
android:layout_width="match_parent"
android:layout_height="48dp"
custombar:leftBackground="@android:color/holo_blue_light"
custombar:leftText="返回"
custombar:leftTextColor="@android:color/black"
custombar:rightBackground="@android:color/holo_blue_light"
custombar:rightText="更多"
custombar:rightTextColor="@android:color/black"
custombar:title="標題欄"
custombar:titleTextColor="@android:color/black"
custombar:titleTextSize="18sp" >
</com.example.mytoolbar.MyToolBar>
</RelativeLayout>
完成這些,運行項目就能看到開頭那張效果圖了。
第四步:為控件添加點擊事件
還差一點——控件尚無點擊響應。先在校件中聲明一個事件接口並提供接口實例:
private OnToolBarClickListener listener;
public interface OnToolBarClickListener {
/** 左邊按鈕點擊事件 */
public void leftClick();
/** 右邊按鈕點擊事件 */
public void rightClick();
}
再暴露一個方法供外部注入監聽器:
public void setOnToolBarClickListener(OnToolBarClickListener listener) {
this.listener = listener;
}
最後在左右按鈕的點擊事件裡回調接口方法即可。聰明的你可能已經猜到,這是典型的回調(監聽器)模式:
leftBtn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
listener.leftClick();
}
});
rightBtn.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
listener.rightClick();
}
});
在 MainActivity 中調用看看:
public class MainActivity extends Activity {
private MyToolBar toolBar;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().hide();
this.toolBar = (MyToolBar) this.findViewById(R.id.mytoolbar);
toolBar.setOnToolBarClickListener(new OnToolBarClickListener() {
@Override public void rightClick() {
Toast.makeText(MainActivity.this, "右邊點擊", Toast.LENGTH_LONG).show();
}
@Override public void leftClick() {
Toast.makeText(MainActivity.this, "左邊點擊", Toast.LENGTH_LONG).show();
}
});
}
}
關於 Android 自定義 UI 控件就介紹到這裡,有任何問題歡迎留言討論。