Android開發(fā)在保證性能的前提下,UI必須要酷酷的可以為你的應(yīng)用加分不少,自定義View的方法中組合控件是經(jīng)常會用到的,所謂自定義都是有套路。
下面以創(chuàng)建常見的TitleBar為例講一下實(shí)現(xiàn)流程~
先上效果圖(很簡單):

shortcut.jpg
步驟:
- 第一步:自定義布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="@dimen/titleBarView_height"
android:paddingEnd="5dp"
android:gravity="center_vertical"
android:background="@android:color/holo_blue_light">
<ImageButton
android:id="@+id/titleBar_leftMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginStart="-10dp"
/>
<TextView
android:id="@+id/titleBar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="@color/white"
android:textSize="@dimen/titleBarView_titleSize"
/>
<ImageButton
android:id="@+id/titleBar_rightMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
/>
</RelativeLayout>
- 第二步:聲明自定義屬性
如果對自定義屬性不熟悉可參考《手摸手教你自定義UI之__自定義控件屬性》
<?xml version="1.0" encoding="utf-8"?><resources>
<declare-styleable name="TitleBarView" >
<attr name="titleName" format="reference|string"/>
<attr name="leftMenuSrc" format="reference|integer"/>
<attr name="rightMenuSrc" format="reference|integer"/>
<attr name="leftMenuVisible" format="boolean"/>
<attr name="rightMenuVisible" format="boolean"/>
</declare-styleable></resources>
- 第三步:代碼邏輯實(shí)現(xiàn)
package com.dailymanager.smartarvin.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.dailymanager.smartarvin.dailymanager.R;
/**
* Todo:自定義TitleBar組合控件
* Author:Arvin
* Date:2016/11/12 22:38
* Email:smartarvin199111@gmail.com
*/
public class TitleBarView extends RelativeLayout {
private Context mContext ;
private OnMenuClickListener mMenuClickListener ;
private int titleResId ;
private int leftMenuSrcId ;
private int rightMenuSrcId ;
private boolean leftMenuVisible = true ;
private boolean rightMenuVisible = true;
private ImageButton leftMenu ;
private TextView title ;
private ImageButton rightMenu ;
public TitleBarView(Context context) {
this(context , null);
}
public TitleBarView(Context context , AttributeSet attrs) {
super(context , attrs );
mContext = context ;
findViews();
initDefaultStyle(context , attrs);
}
private void findViews() {
LayoutInflater.from(mContext).inflate(R.layout.view_titlebar, this, true);
leftMenu = (ImageButton) findViewById(R.id.titleBar_leftMenu);
title = (TextView) findViewById(R.id.titleBar_title);
rightMenu = (ImageButton) findViewById(R.id.titleBar_rightMenu);
leftMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mMenuClickListener != null) {
mMenuClickListener.onLeftMenuClick();
}
}
});
rightMenu.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (mMenuClickListener != null) {
mMenuClickListener.onRightMenuClick();
}
}
});
}
/**
* 初始化原始屬性
* params: Context
* return: void
* throw: null
*/
private void initDefaultStyle(Context context , AttributeSet attrs){
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBarView);
titleResId = a.getResourceId(R.styleable.TitleBarView_titleName , R.string.app_name);
leftMenuSrcId = a.getResourceId(R.styleable.TitleBarView_leftMenuSrc , R.mipmap.ic_launcher);
rightMenuSrcId = a.getResourceId(R.styleable.TitleBarView_rightMenuSrc , R.mipmap.ic_launcher);
leftMenuVisible = a.getBoolean(R.styleable.TitleBarView_leftMenuVisible , true);
rightMenuVisible = a.getBoolean(R.styleable.TitleBarView_rightMenuVisible , true);
a.recycle();
title.setText(titleResId);
leftMenu.setBackgroundResource(leftMenuSrcId);
rightMenu.setBackgroundResource(rightMenuSrcId);
leftMenu.setVisibility(leftMenuVisible ? View.VISIBLE : View.GONE);
rightMenu.setVisibility(rightMenuVisible ? View.VISIBLE : View.GONE);
}
public void setOnMenuClickListener(OnMenuClickListener mMenuClickListener) {
this.mMenuClickListener = mMenuClickListener;
}
/**
* 自定義監(jiān)聽器回調(diào)接口
* params: null
* return: null
* throw: null
*/
public interface OnMenuClickListener {
void onLeftMenuClick();
void onRightMenuClick();
}
}
- 第四步:控件使用
- XML中引用
<com.dailymanager.smartarvin.view.TitleBarView
android:id="@+id/home_titleBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
apps:titleName="@string/app_name"
apps:leftMenuSrc="@mipmap/menu"
apps:rightMenuSrc="@mipmap/more"
apps:leftMenuVisible="true"
apps:rightMenuVisible="true"
/>
- 代碼中使用
private void findViews() {
mTitleBarView = (TitleBarView) findViewById(R.id.home_titleBar);
mTitleBarView.setOnMenuClickListener(new
TitleBarView.OnMenuClickListener() {
@Override
public void onLeftMenuClick() {
Toast.makeText(HomeActivity.this, "Left" , Toast.LENGTH_SHORT).show();
}
@Override
public void onRightMenuClick() {
Toast.makeText(HomeActivity.this, "Right" , Toast.LENGTH_SHORT).show();
}
});
}
自定義組合控件很簡單,不用執(zhí)行重復(fù)繪制view等復(fù)雜邏輯,對于應(yīng)用中會多次重復(fù)使用的控件可以使用該方法。
謹(jǐn)以作為開發(fā)記錄,如果有幫到您,記得點(diǎn)贊哦