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

所属分类: 软件编程 / Android 阅读数: 42
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多