自定义ToolBar

来源:互联网 发布:美敦力公司待遇 知乎 编辑:程序博客网 时间:2024/04/27 13:13

ToolBar

Android 自带的toolBar,有时候并不能满足我们的项目需求,这时候我们就要自定义toolbar来满足我们的需求

目录

[TOC]来生成目录:

  • ToolBar
      • 目录
    • 新建一个布局你想要的布局要满足项目所有toolbar的布局文件

新建一个布局(你想要的布局,要满足项目所有toolbar的布局文件)

这里我给一个简单的布局文件

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="wrap_content">    <TextView        android:id="@+id/left_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:text="111"        android:visibility="gone" />    <TextView        android:id="@+id/title_tv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:text="telte"        android:visibility="gone" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:orientation="horizontal">        <ImageView            android:id="@+id/reight_iv_one"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:visibility="gone" />        <ImageView            android:id="@+id/reight_iv_two"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:visibility="gone" />    </LinearLayout></RelativeLayout>

左侧是个textview 中间是title ,右边的可能有两个imageview

下面的这段代码是自定义的toolbar,需要注意的是要重写setTitle()方法

package com.example.ndk.cainiaoapplication;import android.content.Context;import android.graphics.drawable.Drawable;import android.support.annotation.Nullable;import android.support.annotation.StringRes;import android.support.v7.widget.TintTypedArray;import android.support.v7.widget.Toolbar;import android.util.AttributeSet;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;/** * Created by 17604 on 2016/8/28. * 自定义toolbar * 支持布局,属性扩展  自行扩展 */public class CniaoToolbar extends Toolbar {    private LayoutInflater inflater;    private View mView;    private TextView left_tv;    private TextView title_tv;    private ImageView reight_iv_one;    private ImageView reight_iv_two;    public CniaoToolbar(Context context) {        this(context, null);    }    public CniaoToolbar(Context context, @Nullable AttributeSet attrs) {        this(context, attrs, 0);    }    public CniaoToolbar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView();        if (attrs != null) {            final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,                    R.styleable.CniaoToolbar, defStyleAttr, 0);            final Drawable oneIcon = a.getDrawable(R.styleable.CniaoToolbar_Rigth_icon_one);            final Drawable twoIcon = a.getDrawable(R.styleable.CniaoToolbar_Rigth_icon_two);            if (oneIcon != null) {                setReightOneIcon(oneIcon);            }            if (twoIcon != null) {                setReightTwoIcon(twoIcon);            }            a.recycle();        }    }    private void initView() {        if (mView == null) {            //找到布局            inflater = LayoutInflater.from(getContext());            mView = inflater.inflate(R.layout.activity_main, null);            title_tv = (TextView) mView.findViewById(R.id.title_tv);            left_tv = (TextView) mView.findViewById(R.id.left_tv);            reight_iv_one = (ImageView) mView.findViewById(R.id.reight_iv_one);            reight_iv_two = (ImageView) mView.findViewById(R.id.reight_iv_two);            //添加view            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);            addView(mView, params);        }    }    public void setReightOneIcon(Drawable icon) {        if (reight_iv_one != null) {            showReightOneView();            reight_iv_one.setImageDrawable(icon);        }    }    public void setReightTwoIcon(Drawable icon) {        if (reight_iv_two != null) {            showReightTwoView();            reight_iv_two.setImageDrawable(icon);        }    }    public void setReightOneListener(OnClickListener linstener) {        reight_iv_one.setOnClickListener(linstener);    }    public void setReightTwoListener(OnClickListener linstener) {        reight_iv_two.setOnClickListener(linstener);    }/*** 重新title 方法/    @Override    public void setTitle(@StringRes int resId) {        setTitle(getContext().getText(resId));    }    @Override    public void setTitle(CharSequence title) {        initView();        if (title_tv != null) {            title_tv.setText(title);            showTitleView();        }    }    public void showTitleView() {        if (title_tv != null)            title_tv.setVisibility(VISIBLE);    }    public void showReightOneView() {        if (reight_iv_one != null)            reight_iv_one.setVisibility(VISIBLE);    }    public void hindReightOneView() {        if (reight_iv_one != null)            reight_iv_one.setVisibility(GONE);    }    public void showReightTwoView() {        if (reight_iv_two != null)            reight_iv_two.setVisibility(VISIBLE);    }    public void hindReightTwoView() {        if (reight_iv_two != null)            reight_iv_two.setVisibility(GONE);    }    public void hindTitleView() {        if (title_tv != null)            title_tv.setVisibility(GONE);    }}

我们来看一下toolbar的源码

  final CharSequence title = a.getText(R.styleable.Toolbar_title);        if (!TextUtils.isEmpty(title)) {            setTitle(title);        }

这是toolbar自定义的属性 title ,我们在布局文件中app:title=”“,就可以给标题赋值,在上段代码中我们了重写setTitle方法,来看一下toolbar的

  public void setTitle(@StringRes int resId) {        setTitle(getContext().getText(resId));    }    /**     * Set the title of this toolbar.     *     * <p>A title should be used as the anchor for a section of content. It should     * describe or name the content being viewed.</p>     *     * @param title Title to set     */    public void setTitle(CharSequence title) {        if (!TextUtils.isEmpty(title)) {            if (mTitleTextView == null) {                final Context context = getContext();                mTitleTextView = new AppCompatTextView(context);                mTitleTextView.setSingleLine();                mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);                if (mTitleTextAppearance != 0) {                    mTitleTextView.setTextAppearance(context, mTitleTextAppearance);                }                if (mTitleTextColor != 0) {                    mTitleTextView.setTextColor(mTitleTextColor);                }            }            if (!isChildOrHidden(mTitleTextView)) {                addSystemView(mTitleTextView, true);            }        } else if (mTitleTextView != null && isChildOrHidden(mTitleTextView)) {            removeView(mTitleTextView);            mHiddenViews.remove(mTitleTextView);        }        if (mTitleTextView != null) {            mTitleTextView.setText(title);

很明显
我们只要覆盖这两个方法,用我们自己的逻辑去写就OK,不需要再去自定义属性。attrs.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="CniaoToolbar">        <attr name="Rigth_icon_one" format="reference" />        <attr name="Rigth_icon_two" format="reference" />    </declare-styleable></resources><?xml version="1.0" encoding="utf-8"?><LinearLayout 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="match_parent"    android:orientation="vertical">    <com.example.ndk.cainiaoapplication.CniaoToolbar        android:layout_width="match_parent"        android:layout_height="50dp"        android:background="@color/colorAccent"        app:Rigth_icon_one="@drawable/ic_launcher"        app:Rigth_icon_two="@drawable/ic_launcher"        app:title="主页">    </com.example.ndk.cainiaoapplication.CniaoToolbar></LinearLayout>                                                                                                  这样简单的调用即可,还有好多方法和属性这里就不一一实现了,大家根据自己的需求去实现便可。      
0 0