完整項目下載android
背景:項目中使用標題欄,只是簡單的include一個標題欄的視圖,賦值、控制元素顯示、點擊事件都要本身搞,不優雅!app
要求:函數
1:對現有代碼入侵最小this
2:使用足夠簡單spa
OK,圍繞着這個需求,咱作了一個標準的標題欄。中間有文本,左右兩邊能夠是文字或者是圖片。code
顯示標題欄和左側文字的調用方式以下:orm
<zhexian.app.myapplication.ActionBarLeftRightButton android:layout_width="match_parent" android:layout_height="50dp" app:titleLeftImage="@mipmap/arrow_back" app:titleRightText="提交" app:titleText="當春乃發生"/>
後臺事件呢:控制元素隱藏,設置點擊事件?對象
答案是一句都沒有。66666666blog
怎麼實現的?事件
咱設置了一個Style,文字優先級比圖片高。你設置哪一個屬性,哪一個屬性對應的控件就會顯示。
本控件會判斷載體Context是否實現了click事件,若是是,自動給顯示的控件加上OnClick事件。
<declare-styleable name="ActionBarLeftRightButton"> <attr name="titleLeftText" format="string"/> <attr name="titleLeftImage" format="reference"/> <attr name="titleText" format="string"/> <attr name="titleRightText" format="string"/> <attr name="titleRightImage" format="reference"/> </declare-styleable>
因此後臺你要作的所有,就是在OnClick事件裏面寫對應ID的實現就行了。
實際用下來以後,體驗比之前好多了,優雅~
核心代碼以下:
package zhexian.app.myapplication; import android.content.Context; import android.content.res.TypedArray; import android.graphics.drawable.Drawable; import android.text.TextUtils; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.TextView; /** * 標題欄,左右有按鈕,圖片或者文字均可以 * 優先文字、其次圖片,二者都沒有的話則不顯示 * 參考屬性R.styleable.ActionBarLeftRightButton * Created by 陳俊傑 on 2015/12/8. */ public class ActionBarLeftRightButton extends RelativeLayout { public ActionBarLeftRightButton(Context context) { this(context, null, 0); } public ActionBarLeftRightButton(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ActionBarLeftRightButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); } /** * 若是須要對具體的某個元素進行單獨操做,能夠用本函數得到該對象的引用 * * @param id * @param <T> * @return */ public <T> T getView(int id) { return (T) findViewById(id); } /** * 初始化,須要Context實現了OnClickListener * * @param context * @param attrs */ void initView(Context context, AttributeSet attrs) { View view = LayoutInflater.from(context).inflate(R.layout.view_actionbar_left_right_btn, this, true); setBackgroundResource(R.color.orange); TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.ActionBarLeftRightButton); boolean isHolderCanClick = context instanceof OnClickListener; OnClickListener onClickListener = isHolderCanClick ? (OnClickListener) context : null; bindNavigateAction(view, true, attrArray, onClickListener); bindNavigateAction(view, false, attrArray, onClickListener); String titleText = attrArray.getString(R.styleable.ActionBarLeftRightButton_titleText); if (!TextUtils.isEmpty(titleText)) bindTextView((TextView) view.findViewById(R.id.title_text), titleText, onClickListener); attrArray.recycle(); } /** * 綁定左邊或者右邊的按鈕、文字 * * @param view * @param isLeft * @param attrArray * @param onClickListener */ void bindNavigateAction(View view, boolean isLeft, TypedArray attrArray, OnClickListener onClickListener) { String leftText = attrArray.getString(isLeft ? R.styleable.ActionBarLeftRightButton_titleLeftText : R.styleable.ActionBarLeftRightButton_titleRightText); if (!TextUtils.isEmpty(leftText)) { bindTextView(view, leftText, isLeft, onClickListener); } else { Drawable leftImage = attrArray.getDrawable(isLeft ? R.styleable.ActionBarLeftRightButton_titleLeftImage : R.styleable.ActionBarLeftRightButton_titleRightImage); if (leftImage != null) bindImageView(view, leftImage, isLeft, onClickListener); } } void bindTextView(View view, String text, boolean isLeft, OnClickListener onClickListener) { bindTextView((TextView) view.findViewById(isLeft ? R.id.title_left_text : R.id.title_right_text), text, onClickListener); } /** * 綁定文本 * * @param textView * @param text * @param onClickListener */ void bindTextView(TextView textView, String text, OnClickListener onClickListener) { textView.setVisibility(VISIBLE); textView.setText(text); if (onClickListener != null) textView.setOnClickListener(onClickListener); } /** * 綁定圖片 * * @param view * @param drawable * @param isLeft * @param onClickListener */ void bindImageView(View view, Drawable drawable, boolean isLeft, OnClickListener onClickListener) { ImageButton button = (ImageButton) view.findViewById(isLeft ? R.id.title_left_button : R.id.title_right_button); button.setVisibility(VISIBLE); button.setImageDrawable(drawable); if (onClickListener != null) button.setOnClickListener(onClickListener); } }