自定义Toolbar以及使用

简介: 自定义Toolbar以及使用
public class LlkjToolBar extends Toolbar {
    private View view;
    private TextView toolbarTitle;
    private TextView amRightTv;
    private TextView amLeftTv;
    public static final int isNull = -1;
    //布局
    private LayoutInflater mInflater;
 
    public LlkjToolBar(Context context) {
        this(context, null);
    }
 
    public LlkjToolBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
 
    public LlkjToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //初始化函数
        initView();
        setContentInsetsRelative(0, 0);
        //设置默认销毁当前页面
        if (amLeftTv != null)
            amLeftTv.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((Activity) getContext()).finish();
                }
            });
    }
 
    private void initView() {
        if (view == null) {
            //初始化
            mInflater = LayoutInflater.from(getContext());
            //添加布局文件
            view = mInflater.inflate(R.layout.toolbar, null);
            //绑定控件
            toolbarTitle = (TextView) view.findViewById(R.id.toolbar_title);
            amRightTv = (TextView) view.findViewById(R.id.am_right_tv);
            amLeftTv = (TextView) view.findViewById(R.id.am_left_tv);
            LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_HORIZONTAL);
            addView(view, layoutParams);
        }
    }
 
    //判断是否有头部
    public void initToolBar() {
        setTitle("");
        setEnabled(true);
    }
 
    public void setToolBar(int title, boolean isshowL, int wzidL, int pidL, boolean isshowR, int wzidR, int pidR) {
        initToolBar();
        setTitleText(title);
        if (isshowL) {
            if (wzidL != isNull) {
                setLeftText(wzidL);
            }
            if (pidL != isNull) {
                setLeftImg(pidL);
            }
 
        }
        if (isshowR) {
            if (wzidR != isNull) {
                setRightText(wzidR);
            }
            if (pidR != -1) {
                setRightImg(pidR);
            }
        }
    }
 
    public void setToolBar(String title, boolean isShowL, String wzidL, int pidL, boolean isShowR, String wzidR, int pidR) {
        initToolBar();
        setTitleText(title);
        if (isShowL) {
            if (!TextUtils.isEmpty(wzidL)) {
                setLeftText(wzidL);
            }
            if (pidL != isNull) {
                setLeftImg(pidL);
            }
        }else{
            setLeftButtonOnClickLinster(null);
        }
        if (isShowR) {
            if (!TextUtils.isEmpty(wzidR)) {
                setRightText(wzidR);
            }
            if (pidR != isNull) {
                setRightImg(pidR);
            }
        }
    }
 
    //设置右侧按钮监听事件
    public void setRightButtonOnClickLinster(OnClickListener linster) {
        if (amRightTv != null)
            amRightTv.setOnClickListener(linster);
    }
 
    //设置左侧按钮监听事件
    public void setLeftButtonOnClickLinster(OnClickListener linster) {
        if (amLeftTv != null)
            amLeftTv.setOnClickListener(linster);
    }
 
 
    public void setTitleText(String text) {
        if (toolbarTitle != null)
            toolbarTitle.setText(text);
    }
    public void setTitleTextColor(@ColorRes int color) {
        if (toolbarTitle != null)
            toolbarTitle.setTextColor(ResourceUtil.getColor(getContext(),color));
    }
 
    public void setTitleText(@StringRes int textId) {
        if (toolbarTitle != null)
            toolbarTitle.setText(textId);
    }
 
    public void setTitleBg(@ColorRes int color){
        if(view!=null&&view.findViewById(R.id.toolbarbg)!=null){
            view.findViewById(R.id.toolbarbg).setBackgroundColor(ResourceUtil.getColor(getContext(),color));
        }
    }
 
    //设置左边图片
    public void setLeftImg(@DrawableRes int imgId) {
        if (amLeftTv != null)
            amLeftTv.setCompoundDrawablesWithIntrinsicBounds(imgId, 0, 0, 0);
    }
 
    //设置左边文字
    public void setLeftText(String text) {
        if (amLeftTv != null)
            amLeftTv.setText(text);
    }
    //设置左边字体颜色
    public void setLeftTextColor(@ColorRes int color) {
        if (amLeftTv != null)
            amLeftTv.setTextColor(ResourceUtil.getColor(getContext(),color));
    }
 
    public void setLeftText(@StringRes int textId) {
        if (amLeftTv != null)
            amLeftTv.setText(textId);
    }
 
    //设置右边文字
    public void setRightText(String text) {
        if (amRightTv != null)
            amRightTv.setText(text);
    }
 
    //设置右边字体颜色
    public void setRightTextColor(@ColorRes int color) {
        if (amRightTv != null)
            amRightTv.setTextColor(ResourceUtil.getColor(getContext(),color));
    }
    public void setRightText(@StringRes int textId) {
        if (amRightTv != null)
            amRightTv.setText(textId);
    }
 
    //设置右边图片
    public void setRightImg(@DrawableRes int imgId) {
        if (amRightTv != null)
            amRightTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, imgId, 0);
    }
 
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <com.bloveoa.widget.LlkjToolBar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/toolBarheight"
        android:minHeight="?attr/actionBarSize" />
 
</LinearLayout>
   public void setToolbar(Toolbar mToolbar) {
        setSupportActionBar(mToolbar);
        if (getSupportActionBar() != null) {
            //隐藏标题栏
            getSupportActionBar().setDisplayShowTitleEnabled(false);
        }
    }

2.使用方式

、布局引入

<include layout="@layout/toolbar1" />

  setToolbar(mToolbar);
        mToolbar.setToolBar(R.string.sign, true, LlkjToolBar.isNull, R.drawable.ic_back, true, LlkjToolBar.isNull, LlkjToolBar.isNull);

3. R.layout.toolbar

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:id="@+id/toolbarbg"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:paddingTop="@dimen/paddingtop"
             android:background="@color/title_bg"
             >
    <TextView
        android:id="@+id/am_left_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="?attr/actionBarItemBackground"
        android:clickable="true"
        android:gravity="left|center_vertical"
        android:minWidth="@dimen/dp_50"
        android:paddingLeft="15dp"
        android:textColor="@color/colorPrimary"
        tools:text="edit"/>
 
    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="我的App"
        android:textColor="@android:color/white"
        android:textSize="18sp"/>
 
    <TextView
        android:id="@+id/am_right_tv"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:background="?attr/actionBarItemBackground"
        android:clickable="true"
        android:gravity="right|center_vertical"
        android:minWidth="@dimen/dp_50"
        android:paddingRight="@dimen/dp_15"
        android:textColor="@color/white"
        tools:text="edit"/>
</FrameLayout>
相关文章
|
4月前
自定义tablayout,好用
自定义tablayout,好用
25 0
|
4月前
自定义tablayout中的tab样式
自定义tablayout中的tab样式
23 0
|
3月前
|
XML IDE 开发工具
【Android UI】自定义带按钮的标题栏
【Android UI】自定义带按钮的标题栏
47 7
【Android UI】自定义带按钮的标题栏
|
4月前
|
XML Android开发 数据格式
Android下自定义Button样式
Android下自定义Button样式
37 3
|
Java Android开发 Kotlin
Android 自定义加载动画Dialog弹窗
Android 自定义加载动画Dialog弹窗
326 0
Android 自定义加载动画Dialog弹窗
|
Android开发 容器
Android动态修改ToolBar的Menu菜单
Android动态修改ToolBar的Menu菜单 效果图 实现 实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,把对应的Action隐藏了。
1772 0
|
XML 数据格式
自定义Toolbar的一些小技巧
1、改变Toolbar高度(解决图标不垂直居中) 背景:实际使用中,toolbar默认高度有些大,会挤压内容。想将toolbar高度改小,将layout_height从wrap_content改为固定值。 情况:toolbar的layout_height比默认高度小的时候,发现标题是居中的,但是两侧的图标不垂直居中而偏下了 调查:网上有很多方法,基本都是无效的。甚至有人利用反射直接修改图标的imageview的gravity,非常复杂且右侧图标无法实现。
236 0
Qml-Dialog不能隐藏标题栏和按钮自定义
在项目中,需要弹出一个对话框来完成用户输入的功能,为了考虑界面的同一,这里需要将原生自带的标题栏隐藏掉,换成自己写的
564 0
|
Android开发 容器 数据格式
TabLayout的自定义
TabLayout的自定义,主要是通过setCustomView方法来添加自定义布局实现。 自定义TabLayout的实现主要包含以下几个步骤 ●创建自定义布局(这里我加了一个动画控件,可以替换成其他控件) ...
2007 0