iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android ToolBar整合实例使用方法详解
  • 328
分享到

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

方法toolbarAndroid 2022-06-06 04:06:21 328人浏览 泡泡鱼
摘要

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

最近做项目中遇到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;

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动态修改ToolBar的Menu菜单示例Android中Toolbar随着ScrollView滑动透明度渐变效果实现Android折叠式Toolbar使用完全解析(CollapsingToolbarLayout)Android自定义Toolbar使用方法详解Android ToolBar控件详解及实例Android自定义ActionProvider ToolBar实现Menu小红点解决Android V7后自定义Toolbar、ActionBar左侧有空白问题Android5.0+ CollapsingToolbarLayout使用详解Android中ActionBar和ToolBar添加返回箭头的实例代码android ToolBar的简单使用


--结束END--

本文标题: Android ToolBar整合实例使用方法详解

本文链接: https://www.lsjlt.com/news/22240.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • ToolBar使用方法详解
    ToolBar的出现是为了替换之前的ActionBar的各种不灵活使用方式,相反,ToolBar的使用变得非常灵活,因为它可以让我们自由往里面添加子控件.低版本要使用的话,可以添加support-v7包. 今天要实现的效果如下: 由上图可以...
    99+
    2023-05-30
    android toolbar
  • 利用Android如何实现对 ToolBar进行整合
    本篇文章给大家分享的是有关利用Android如何实现对 ToolBar进行整合,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。CustomeToolBar继承原生ToolBarp...
    99+
    2023-05-31
    android toolbar roi
  • 实例详解Android中JNI的使用方法
    目录前言1.导入C语言的类2.接着导入Android.mk文件3.我们配置一下build.gradle文件4.好了,此时可以编译一下项目了6.将生成的so文件拷入src/main/j...
    99+
    2024-04-02
  • Android ExpandableListView使用方法案例详解
    目录一、前言二、实现的功能三、具体代码1、主xml代码2、父布局xml代码3、子布局xml代码4、主activity代码5、adapter代码一、前言   “好记性不如烂笔...
    99+
    2024-04-02
  • springBoot整合redis使用案例详解
    一、创建springboot项目(采用骨架方式) 创建完成; 我们分析下pom文件中内容: 所使用到的关键依赖: <!--springBoot集成redis-...
    99+
    2024-04-02
  • SpringBoot整合Canal方法详解
    目录pom.xml 添加 canal.client 依赖业务功能处理简单连接程序单次获取数据循环获取数据解析 storeValue 值不同的类型进行不同的处理一次性获取多条数据ack...
    99+
    2022-12-21
    SpringBoot整合Canal SpringBoot Canal
  • SpringBoot整合Quartz方法详解
    目录基础依赖cron表达式通用内存任务工程启动时就在执行的任务手动控制某个任务定义任务借助Web-Controller去开启该任务持久化配置Quartz为我们准备了sql数据表暂停任...
    99+
    2023-05-17
    SpringBoot整合Quartz SpringBoot Quartz
  • Spring Boot 整合 Reactor实例详解
    目录引言1 创建项目2 集成 H2 数据库3 创建测试类3.1 user 实体3.2 UserRepository3.3 UserService3.4 UserController3...
    99+
    2024-04-02
  • SpringBoot整合Shiro的方法详解
    目录1.Shito简介1.1 什么是shiro1.2 有哪些功能2.QuickStart3.SpringBoot中集成1.导入shiro相关依赖2.自定义UserRealm3.定义s...
    99+
    2024-04-02
  • springBoot整合rabbitMQ的方法详解
    引入pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave...
    99+
    2024-04-02
  • SpringBoot整合mybatis的方法详解
    目录1 依赖配置2 使用2.1 SpringBoot配置整合mybatis:2.2 SpringBoot注解整合mybatis:2.3 在配置类上增加@MapperScan注解,扫描...
    99+
    2024-04-02
  • springboot整合mybatisplus的方法详解
    目录POM:application.yaml:POJO:mapper接口:包扫描:测试:总结POM: <dependency> <groupId>com....
    99+
    2024-04-02
  • Android IdleHandler使用方法详解
    正文 在Android中,Handler是一个使用的非常频繁的东西,输入事件机制和系统状态,都通过Handler来进行流转,而在Handler中,有一个很少被人提起但是却很有用的东西...
    99+
    2022-11-13
    Android IdleHandler Android IdleHandler使用
  • Android Notification 使用方法详解
    Android Notification 使用方法详解用TaskStackBuilder来获取PendingIntent处理点击跳转到别的Activity,首先是用一般的PendingIntent来进行跳转。mBuilder = new N...
    99+
    2023-05-30
    android notification roi
  • Android HandlerThread使用方法详解
    Android HandlerThread使用方法详解HandlerThread 继承自Thread,内部封装了Looper。首先Handler和HandlerThread的主要区别是:Handler与Activity在同一个线程中,Han...
    99+
    2023-05-30
    android handlerthread roi
  • ssm整合shiro使用详解
    目录整合shiro:1.在pom.xml中引入依赖2.新建并配置缓存ehcache.xml3.在spring配置文件applicationContext.xml配置shiro4.自定...
    99+
    2024-04-02
  • Spring利用注解整合Mybatis的方法详解
    目录一、环境准备步骤1:数据库相关步骤2:导入jar包步骤3:创建模型类步骤4:创建Dao接口和实现类步骤5:创建Service接口和实现类步骤6:添加jdbc.properties...
    99+
    2024-04-02
  • SpringBoot实现整合微信支付方法详解
    目录1.准备工作1.1 数据库表1.2 实体类1.3 导入依赖1.4 配置文件1.5 创建读取微信支付相关信息的工具类1.6 其他工具类2.生成订单2.1 远程调用用户模块和课程模块...
    99+
    2024-04-02
  • apache zookeeper使用方法实例详解
    本文涉及了Apache Zookeeper使用方法实例详解的相关知识,接下来我们就看看具体内容。简介Apache Zookeeper 是由 Apache Hadoop 的 Zookeeper 子项目发展而来,现在已经成为了 Apache 的...
    99+
    2023-05-31
    apache zookeeper
  • Spring Boot整合Lombok的方法详解
    上篇文章给大家介绍了,喜欢的朋友点击查看下。 SpringBoot 开发提速神器 Lombok+MybatisPlus+SwaggerUI Lombok为啥这么牛逼?Spri...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作