Android自定义Toolbar使用方法详解

作者:java是最好的语言 时间:2022-03-26 14:47:10 

本篇文章介绍:

如何使用Toolbar;

自定义Toolbar;

先来看一看效果,了解一下toolbar;

Android自定义Toolbar使用方法详解

布局文件:


<android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@color/colorPrimary"/>

Actvity中设置属性:


Toolbar toolBar= (Toolbar) findViewById(R.id.toolbar);
toolBar.setLogo(R.mipmap.ic_launcher);//设置图标
toolBar.setTitle("Title");//设置主标题
toolBar.setSubtitle("smalltitle");//设置子标题

这样就可以实现上面的效果。

接下来是自定义的Toolbar:

Android自定义Toolbar使用方法详解

布局文件:


<com.example.cjj.test.bean.MyToolBar
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="?attr/colorPrimary"
   android:minHeight="?attr/actionBarSize"
   android:layout_centerInParent="true"
   android:layout_gravity="center"
 >
 </com.example.cjj.test.bean.MyToolBar>

toolbar.xml:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
 android:layout_height="match_parent">

<ImageButton
   android:id="@+id/mLeftButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentLeft="true"
   android:layout_centerVertical="true"
   android:background="?attr/colorPrimary"
   />
 <TextView
   android:id="@+id/toolbar_title"
   android:text="title"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:layout_centerInParent="true"
   android:layout_gravity="center"
   android:gravity="center"
   android:textColor="@color/white"
   android:textSize="20sp"
   />
 <ImageButton
   android:id="@+id/mRightButton"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentRight="true"
   android:layout_centerVertical="true"
   android:background="?attr/colorPrimary"/>
</RelativeLayout>

新建一个MyToolbar:


public class MyToolBar extends Toolbar {
 //布局
 private LayoutInflater mInflater;
 //右边按钮
 private ImageButton mRightButton;
 //左边按钮
 private ImageButton mLeftButton;
 //标题
 private TextView mTextTitle;

private View view;

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

public MyToolBar(Context context, AttributeSet attrs) {
   this(context, attrs, 0);
 }
 public MyToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
   super(context, attrs, defStyleAttr);

//初始化函数
   initView();
   setContentInsetsRelative(10, 10);
   if (attrs != null) {
      setLeftButtonIcon(R.mipmap.back_icon);//设置左图标
       //设置点击事件
       setLeftButtonOnClickLinster(new OnClickListener() {
         @Override
         public void onClick(View v) {
           Toast.makeText(getContext(),"left",Toast.LENGTH_SHORT).show();
         }
       });
       setRightButtonIcon(R.mipmap.nav_more);//设置右图标
        //设置点击事件
       setRightButtonOnClickLinster(new OnClickListener() {
         @Override
         public void onClick(View v) {
           Toast.makeText(getContext(), "right", Toast.LENGTH_SHORT).show();
         }
       });
   }
 }
private void initView() {
   if(view==null){
     //初始化
     mInflater= LayoutInflater.from(getContext());
     //添加布局文件
     view=mInflater.inflate(R.layout.toolbar,null);
     //绑定控件
     mEditSearchView= (EditText) view.findViewById(R.id.toolbar_searchview);
     mTextTitle= (TextView) view.findViewById(R.id.toolbar_title);
     mLeftButton= (ImageButton) view.findViewById(R.id.mLeftButton);
     mRightButton= (ImageButton) view.findViewById(R.id.mRightButton);

LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
     addView(view, layoutParams);
   }
 }
  public void setRightButtonIcon(int icon){

if(mRightButton !=null){

mRightButton.setImageResource(icon);
     // mRightButton.setVisibility(VISIBLE);
   }

}
 public void setLeftButtonIcon(int icon){

if(mLeftButton !=null){

mLeftButton.setImageResource(icon);
     //mLeftButton.setVisibility(VISIBLE);
   }

}

//设置右侧按钮监听事件
 public void setRightButtonOnClickLinster(OnClickListener linster) {
   mRightButton.setOnClickListener(linster);
 }

//设置左侧按钮监听事件
 public void setLeftButtonOnClickLinster(OnClickListener linster) {
   mLeftButton.setOnClickListener(linster);
 }
标签:Android,Toolbar
0
投稿

猜你喜欢

  • 线程池ThreadPoolExecutor并行处理实现代码

    2022-10-13 23:44:01
  • C#如何安全、高效地玩转任何种类的内存之Span的本质

    2022-10-23 10:59:15
  • Mybatis-Plus之ID自动增长的设置实现

    2022-10-27 00:09:47
  • Java监听器ActionListener与MouseListener的执行顺序说明

    2022-02-04 20:08:23
  • c# 反射用法及效率对比

    2022-06-11 17:50:19
  • java设计模式之外观模式学习笔记

    2022-07-02 18:48:06
  • java字符串常用操作方法(查找、截取、分割)

    2023-11-29 03:21:13
  • 用JAVA实现杨辉三角实例

    2023-08-28 16:45:23
  • 详解C#如何实现读写ini文件

    2022-02-04 23:15:27
  • java实现简单的英文文本单词翻译器功能示例

    2023-11-28 10:22:15
  • Unity Shader实现黑幕过场效果

    2022-01-13 00:18:10
  • C#中DataTable排序、检索、合并等操作实例

    2022-08-22 04:58:03
  • 分享我的第一次java Selenium自动化测试框架开发过程

    2021-05-30 01:16:25
  • SpringMVC实现表单验证功能详解

    2023-09-24 09:07:28
  • spring boot集成p6spy的最佳实践

    2023-04-11 23:40:36
  • Spring MVC 学习 之 - URL参数传递详解

    2022-03-04 22:05:12
  • 浅谈Java编程中的内存泄露情况

    2022-09-11 06:59:07
  • Springboot整合微信支付(订单过期取消及商户主动查单)

    2023-05-15 23:40:50
  • 最好的8个Java RESTful框架

    2023-02-07 07:49:44
  • C# ADO.NET 离线查询的实现示例

    2023-06-12 00:52:03
  • asp之家 软件编程 m.aspxhome.com