Android自定义Toolbar使用方法详解

本篇文章介绍:

如何使用Toolbar;

自定义Toolbar;

先来看一看效果,了解一下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:

布局文件:

<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); }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-07-28 17:41:00

Android自定义Toolbar使用方法详解的相关文章

Android 中 onSaveInstanceState()使用方法详解

Android 中 onSaveInstanceState()使用方法详解 覆盖onSaveInstanceState方法,并在onCreate中检测savedInstanceState和获取保存的值 @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("currentposition", videoView.getCurrentPosition()); super.onSave

Android Parcelable接口使用方法详解

Android Parcelable接口使用方法详解 1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing

Android StringBuffer的使用方法详解

今天,讲讲StringBuffer的使用. StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类. 所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入.删除等操作,使用StringBuffer要更加适合一些. 在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能

Android组件popupwindow使用方法详解_Android

先看效果:  现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的  上源码 main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="

AngularJS创建自定义指令的方法详解_AngularJS

本文实例讲述了AngularJS创建自定义指令的方法.分享给大家供大家参考,具体如下: 这是一篇译文,来自angular开发者说明的指令.主要面向已经熟悉angular开发基础的开发者.这篇文档解释了什么情况下需要创建自己的指令,和如何去创建指令. 什么是指令 从一个高的层面来讲,指令是angular $compile服务的说明,当特定的标签(属性,元素名,或者注释) 出现在DOM中的时候,它让编译器附加指定的行为到DOM上. 这个过程是很简单的.angular内部有很用这样自带的指令,比如说n

Android中HorizontalScrollView使用方法详解_Android

由于移动设备物理显示空间一般有限,不可能一次性的把所有要显示的内容都显示在屏幕上.所以各大平台一般会提供一些可滚动的视图来向用户展示数据.Android平台框架中为我们提供了诸如ListView.GirdView.ScrollView等滚动视图控件,这几个视图控件也是我们平常使用最多的.下面介绍一下HorizontalScrollView的使用和需要注意的点:  HorizontalScrollView是一个FrameLayout  ,这意味着你只能在它下面放置一个子控件,这个子控件可以包含很多

Android编程实现自定义手势的方法详解_Android

本文实例讲述了Android编程实现自定义手势的方法.分享给大家供大家参考,具体如下: 之前介绍过如何在Android程序中使用手势,主要是系统默认提供的几个手势,这次介绍一下如何自定义手势,以及如何对其进行管理. 先介绍一下Android系统对手势的管理,Android系统允许应用程序把用户的手势以文件的形式保存以前,以后要使用这些手势只需要加载这个手势库文件即可,同时Android系统还提供了诸如手势识别.查找及删除等的函数接口,具体如下: 一.加载手势库文件: staticGestureL

Android编程实现自定义手势的方法详解

本文实例讲述了Android编程实现自定义手势的方法.分享给大家供大家参考,具体如下: 之前介绍过如何在Android程序中使用手势,主要是系统默认提供的几个手势,这次介绍一下如何自定义手势,以及如何对其进行管理. 先介绍一下Android系统对手势的管理,Android系统允许应用程序把用户的手势以文件的形式保存以前,以后要使用这些手势只需要加载这个手势库文件即可,同时Android系统还提供了诸如手势识别.查找及删除等的函数接口,具体如下: 一.加载手势库文件: staticGestureL

Android实现在ServiceManager中加入自定义服务的方法详解

本文实例讲述了Android实现在ServiceManager中加入自定义服务的方法.分享给大家供大家参考,具体如下: 当我们要使用android的系统服务时,一般都是使用Context.getSystemService方法.例如我们要获取AudioManager,我们可以: AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 获取的服务,其实是在ServiceManager中注册的Binder服务,