Android ToolBar整合实例使用方法详解

所属分类: 软件编程 / Android 阅读数: 17
收藏 0 赞 0 分享

最近做项目中遇到ToolBar因为不同的界面toobar不同为了描述统一的风格。相信大家也非常清楚,大多数ToolBar包括以下几个方面

  • 左标题 左边题颜色 左标题图标等
  • 标题 标题颜色
  • 右标题 右标题颜色 右标题图标
  • ToolBar标题 ToolBar颜色 ToolBar图标
  • ToolBar子标题 ToolBar子标题 ToolBar子标题颜色

再看一下淘宝以及其他appToolBar样式界面

下面看下我自定义的CustomeToolBar继承原生ToolBar

package com.ldm.imitatewx;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;

/**
 * Created by John on 2017/2/16.
 */

public class CustomeToolBar extends Toolbar {
 private TextView mTvMainTitleLeft;
 private TextView mTvMainTitle;
 private TextView mTvMainRight;
 public CustomeToolBar(Context context) {
 super(context);
 }

 public CustomeToolBar(Context context, AttributeSet attrs) {
 super(context, attrs);
 }

 public CustomeToolBar(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 }

 @Override
 protected void onFinishInflate() {
 super.onFinishInflate();
 mTvMainTitleLeft= (TextView) findViewById(R.id.lt_main_title_left);
 mTvMainTitle= (TextView) findViewById(R.id.lt_main_title);
 mTvMainRight= (TextView) findViewById(R.id.lt_main_title_right);
 }
 //设置主title内容
 public void setMainTitle( String text )
 {
 this.setTitle(" ");
 mTvMainTitle.setVisibility(View.VISIBLE);
 mTvMainTitle.setText(text);
 }
 //设置主title的内容文字的颜色
 public void setTitleColor(int color )
 {
 mTvMainTitle.setTextColor(color);
 }
 //设置左边title内容
 public void setMainTitleLeft(String text )
 {
 mTvMainTitleLeft.setVisibility(View.VISIBLE);
 mTvMainTitleLeft.setText(text);
 }
 //设置左边的title颜色
 public void setMainTitleLeftColor(int color )
 {
 mTvMainTitleLeft.setTextColor(color);
 }
 //设置左边icon
 public void setMainTitleLeftDrawable(int res )
 {
 Drawable left= ContextCompat.getDrawable(getContext(),res);
 left.setBounds(0,0,left.getMinimumWidth(),left.getMinimumHeight());
 mTvMainTitleLeft.setCompoundDrawables(left,null,null,null);
 }
 //设置右边的title
 public void setTvMainRightText(String text )
 {
 mTvMainRight.setVisibility(View.VISIBLE);
 mTvMainRight.setText(text);
 }
 //设置右边标题的颜色
 public void setMainTitleRightColor(int color )
 {
 mTvMainRight.setTextColor(color);
 }
 //设置右边icon
 public void setMainTitleRightDrawable(int res )
 {
 Drawable right= ContextCompat.getDrawable(getContext(),res);
 right.setBounds(0,0,right.getMinimumWidth(),right.getMinimumHeight());
 mTvMainTitleLeft.setCompoundDrawables(right,null,null,null);
 }
 //设置toolbar颜色
 public void setToolBarBackground(int res )
 {
 this.setBackgroundResource(res);
 }
 //设置ToolBar左边的图标
 public void setToolbarLeftBackImageRes(int res )
 {
 this.setNavigationIcon(res);
 }
 //设置toolbar左边文字
 public void setToolbarLeftText(String text ){
 this.setNavigationContentDescription(text);
 }
 //设置toolbar标题
 public void setToolbarTitle(String text )
 {
 this.setTitle(text);
 }
 //设置toolbar颜色
 public void setToolbarTitleColor(int color )
 {
 this.setTitleTextColor(color);
 }
 //设置ToolBar子标题
 public void setToolbarSubTitleText(String text )
 {
 this.setSubtitle(text);
 }
 //设置toolbar子标题的颜色
 public void setToolbarSubTitleTextColor(int color )
 {
 this.setSubtitleTextColor(color);
 }

}

然后布局引用activity_custome_toolbar
因为其实toolbar说白也是view也可以说是一个布局
所以我们只要根据自己需求往里面丢东西就ok,这里可能不全面,希望大家一起完善谢谢!

<?xml version="1.0" encoding="utf-8"?>
<com.ldm.imitatewx.CustomeToolBar 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="?attr/label_textSize"
 android:background="@android:color/holo_green_light"
 android:fitsSystemWindows="true"
 app:contentInsetLeft="0dp"
 app:contentInsetStart="0dp"
 app:popupTheme="@style/MyPopStyle"
 >
 <TextView
 android:id="@+id/lt_main_title_left"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="10dp"
 android:text="返回"
 android:gravity="center"
 android:drawableLeft="@drawable/ic_back_u"
 android:textColor="@android:color/white"
 android:singleLine="true"
 android:textSize="16sp"
 android:visibility="visible"/>
 <TextView
 android:id="@+id/lt_main_title"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:singleLine="true"
 android:textColor="@android:color/white"
 android:text="标题"
 android:textSize="20sp"
 android:visibility="visible"
 />
 <TextView
 android:id="@+id/lt_main_title_right"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="right"
 android:layout_marginRight="10dp"
 android:text="返回"
 android:gravity="center"
 android:drawableRight="@drawable/ic_add"
 android:textColor="@android:color/white"
 android:singleLine="true"
 android:textSize="16sp"
 android:visibility="visible"/>

</com.ldm.imitatewx.CustomeToolBar>

到这里基本结束了!大家可以继续完善!谢谢!

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

更多精彩内容其他人还在看

Android实现悬浮窗体效果

这篇文章主要为大家详细介绍了Android实现悬浮窗体效果,显示悬浮窗口,窗口可以拖动,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Andriod studio 打包aar 的方法

这篇文章主要介绍了Andriod studio 打包aar的方法,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Android加载loading对话框的功能及实例代码(不退出沉浸式效果)

这篇文章主要介绍了Android加载loading对话框的功能及实例代码,不退出沉浸式效果,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中LayoutInflater.inflater()的正确打开方式

这篇文章主要给大家介绍了关于Android中LayoutInflater.inflater()的正确打开方式,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Delphi在Android下使用Java库的方法

这篇文章主要介绍了Delphi在Android下使用Java库的方法,本文以Android的USB串口通讯库为例,给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Retrofit2日志拦截器的使用

这篇文章主要介绍了Retrofit2日志拦截器的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android创建外部lib库及自定义View的图文教程

这篇文章主要给大家介绍了关于Android创建外部lib库及自定义View的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序失败的一些事小结

这篇文章主要给大家介绍了关于Android分享微信小程序失败一些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android分享微信小程序技巧之图片优化

这篇文章主要给大家介绍了关于Android分享微信小程序技巧之图片优化的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android Viewpager实现无限循环轮播图

这篇文章主要为大家详细介绍了Android Viewpager实现无限循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多