Android开发经验谈Android开发Android技术知识

快速开发之-在项目里定义一个通用titleBar

2018-09-03  本文已影响23人  lance_小超

声明

本文并非教程,而是笔记,不会对知识点一个一个的解释,当然如果能帮到小伙伴,我也会感到很欣慰,有好的意见以及方案,可以回复

目的

免除重复XML代码编写,继承Toolbar
可拓展性,其他特殊标题继承titleBar可以轻松拓展
方便全栈样式统一

知识点

Toolbar
ViewGroup

很简单 直接附上代码 只是最基础版的 根据实际项目需要修改

CommonTitleBar

public class CommonTitleBar extends Toolbar {

    private String titleName;
    private int leftButtonBackground;
    private int rightButtonBackground;

    private TextView toolbarTitle;
    private ImageButton toolbarButtonLeft;
    private ImageButton toolbarButtonRight;

    public CommonTitleBar(Context context) {
        this(context, null);
    }

    public CommonTitleBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CommonTitleBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(context).inflate(R.layout.common_titlebar_layout, this);
        initAttr(attrs);
        initView();
    }

    private void initAttr(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CommonTitleBar);
        titleName = typedArray.getString(R.styleable.CommonTitleBar_titleName);
        leftButtonBackground = typedArray.getInt(R.styleable.CommonTitleBar_leftButtonBackground, R.mipmap.toolbar_back);
        rightButtonBackground = typedArray.getInt(R.styleable.CommonTitleBar_rightButtonBackground, 0);
    }

    private void initView() {
        toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        toolbarButtonLeft = (ImageButton) findViewById(R.id.toolbar_button_left);
        toolbarButtonRight = (ImageButton) findViewById(R.id.toolbar_button_right);

        setTitleName(titleName);
        setLeftButtonBackground(leftButtonBackground);
        setRightButtonBackground(rightButtonBackground);

    }


    /**
     * 设置标题
     *
     * @param titleName
     */
    public void setTitleName(String titleName) {

        toolbarTitle.setText(titleName);

    }

    /**
     * 设置左边图标
     *
     * @param leftButtonBackground
     */
    public void setLeftButtonBackground(int leftButtonBackground) {
        if(leftButtonBackground!=0) toolbarButtonLeft.setBackgroundResource(leftButtonBackground);

    }

    /**
     * 设置右边图标
     *
     * @param rightButtonBackground
     */
    public void setRightButtonBackground(int rightButtonBackground) {
        if(rightButtonBackground!=0) toolbarButtonRight.setBackgroundResource(rightButtonBackground);
    }

    /**
     * 左边按钮点击监听 已添加防止重复点击
     * @param mLeftClick
     */
    public void setmLeftClick(final onViewClick mLeftClick) {
        if(mLeftClick!=null) toolbarButtonLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AntiShake.check(v.getId())){
                    mLeftClick.onViewClick();
                }
            }
        });
    }

    /**右边左边按钮点击监听 已添加防止重复点击
     * @param mRightClick
     */
    public void setmRightClick(final onViewClick mRightClick) {
        if(mRightClick!=null) toolbarButtonLeft.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(AntiShake.check(v.getId())){
                    mRightClick.onViewClick();
                }
            }
        });
    }


    private interface onViewClick {
        void onViewClick();
    }
}

common_titlebar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    app:contentInsetStart="0dp"
    android:background="#FFFFFF">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp">
        <TextView
            android:id="@+id/toolbar_title"
            android:textSize="18dp"
            android:textColor="#666666"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:layout_marginLeft="60dp"
            android:layout_marginRight="60dp"
            android:lines="1"
            android:ellipsize="end"
            android:text="标题" />
        <ImageButton
            android:id="@+id/toolbar_button_left"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:background="@mipmap/toolbar_back"/>
        <ImageButton
            android:id="@+id/toolbar_button_right"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:background="@mipmap/toolbar_share" />
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

attrs

<!--通用titleBar-->
    <declare-styleable name="CommonTitleBar">
        <attr name="titleName" format="string|reference"/>
        <attr name="leftButtonBackground" format="reference"/>
        <attr name="rightButtonBackground" format="reference"/>
    </declare-styleable>
上一篇下一篇

猜你喜欢

热点阅读