【android】实现一个自己的标题栏

完整项目下载

背景:项目中使用标题栏,只是简单的include一个标题栏的视图,赋值、控制元素显示、点击事件都要自己搞,不优雅!

要求:

1:对现有代码入侵最小

2:使用足够简单

OK,围绕着这个需求,咱做了一个标准的标题栏。中间有文本,左右两边可以是文字或者是图片。

显示标题栏和左侧文字的调用方式如下:

 <zhexian.app.myapplication.ActionBarLeftRightButton
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:titleLeftImage="@mipmap/arrow_back"
        app:titleRightText="提交"
        app:titleText="当春乃发生"/>

 

后台事件呢:控制元素隐藏,设置点击事件?

答案是一句都没有。66666666

怎么实现的?

咱设置了一个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);
    }
}

 

posted @ 2015-12-09 21:07  谪仙  阅读(487)  评论(0编辑  收藏  举报