Android ToolBar+DrawerLayout的实现

2024-09-08 08:48

本文主要是介绍Android ToolBar+DrawerLayout的实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

实现思路:
1.关联相应的依赖包

compile ‘com.android.support:appcompat-v7:25.3.1’

2.主布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context="com.example.toolbar.MainActivity" >  <include layout="@layout/toolbar" />  <android.support.v4.widget.DrawerLayout  android:id="@+id/drawer"  android:layout_width="match_parent"  android:layout_height="match_parent" >  <!-- 内容界面 -->  <LinearLayout  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical" >  <com.example.toolbar.widget.PagerSlidingTabStrip  android:id="@+id/tabs"  android:layout_width="match_parent"  android:layout_height="48dip" >  </com.example.toolbar.widget.PagerSlidingTabStrip>  <android.support.v4.view.ViewPager  android:id="@+id/pager"  android:layout_width="match_parent"  android:layout_height="match_parent" >  </android.support.v4.view.ViewPager>  </LinearLayout>  <!-- 侧滑菜单内容 -->  <LinearLayout  android:id="@+id/drawer_view"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:layout_gravity="start"  android:background="@drawable/drawer"  android:orientation="vertical"  android:padding="8dp" >  <TextView  android:layout_width="match_parent"  android:layout_height="match_parent" />  </LinearLayout>  </android.support.v4.widget.DrawerLayout>  </LinearLayout>  

Toolbar布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
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="wrap_content"
android:background="?attr/colorPrimary"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"></android.support.v7.widget.Toolbar>

Toolbar条上item的布局

<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"tools:context=".MainActivity" ><item android:id="@+id/action_edit"android:title="edit"android:orderInCategory="80"android:icon="@mipmap/ic_launcher"app:showAsAction="ifRoom" /><item android:id="@+id/action_share"android:title="share"android:orderInCategory="90"android:icon="@mipmap/ic_launcher"app:showAsAction="ifRoom" /><item android:id="@+id/action_settings"android:title="settings"android:orderInCategory="100"app:showAsAction="never"/></menu>

3.需要在style文件中,设置noActiobBar

<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"><!-- Customize your theme here. --><item name="colorPrimary">@color/colorPrimary</item><item name="colorPrimaryDark">@color/colorPrimaryDark</item><item name="colorAccent">@color/colorAccent</item></style></resources>

4.在Activity中去找到ToolBar控件,然后关联ActionBar操作

mToolBar = (Toolbar) this.findViewById(R.id.toolbar);mToolBar.setTitle("XHTest");setSupportActionBar(mToolBar);// 3.设置返回箭头getSupportActionBar().setDisplayHomeAsUpEnabled(true);ActionBarDrawerToggle toggle =  new ActionBarDrawerToggle(this,mDrawer,mToolBar,R.string.app_name,R.string.app_name);toggle.syncState();mDrawer.addDrawerListener(toggle);mToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {@Overridepublic boolean onMenuItemClick(MenuItem item) {switch(item.getItemId()) {
//                    case R.id.action_search:
//                        Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show();
//                        break;case R.id.action_edit:Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show();break;case R.id.action_settings:Toast.makeText(getApplicationContext(), "设置", Toast.LENGTH_SHORT).show();break;case R.id.action_share:Toast.makeText(getApplicationContext(), "分享", Toast.LENGTH_SHORT).show();break;default:break;}return true;}});

5.注意,要调用onCreateOptionsMenu方法,ToolBar上的item才会显示

// 1.必须创建菜单,才会显示@Overridepublic boolean onCreateOptionsMenu(Menu menu) {MenuInflater inflater = getMenuInflater();inflater.inflate(R.menu.toolbar_item,menu);return super.onCreateOptionsMenu(menu);}

6.Palette的使用
6.1 首先关联一个依赖包

compile ‘com.android.support:palette-v7:23.4.0’

6.2 使用Palette的异步方法

private void changeBackgroundColor(int position) {
// 用来提取颜色的Bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), PaletteFragment.getBackgroundBitmapPosition(position));
// Palette的部分
Palette.Builder builder = Palette.from(bitmap);
builder.generate(new Palette.PaletteAsyncListener() {@Override public void onGenerated(Palette palette) {//获取到充满活力的这种色调Palette.Swatch vibrant = palette.getVibrantSwatch();//根据调色板Palette获取到图片中的颜色设置到toolbar和tab中背景,标题等,使整个UI界面颜色统一toolbar_tab.setBackgroundColor(vibrant.getRgb());toolbar_tab.setSelectedTabIndicatorColor(colorBurn(vibrant.getRgb()));toolbar.setBackgroundColor(vibrant.getRgb());if (android.os.Build.VERSION.SDK_INT >= 21) {Window window = getWindow();window.setStatusBarColor(colorBurn(vibrant.getRgb()));window.setNavigationBarColor(colorBurn(vibrant.getRgb()));}}
});};/*** 颜色加深处理** @param RGBValues*            RGB的值,由alpha(透明度)、red(红)、green(绿)、blue(蓝)构成,*            Android中我们一般使用它的16进制,*            例如:"#FFAABBCC",最左边到最右每两个字母就是代表alpha(透明度)、*            red(红)、green(绿)、blue(蓝)。每种颜色值占一个字节(8位),值域0~255*            所以下面使用移位的方法可以得到每种颜色的值,然后每种颜色值减小一下,在合成RGB颜色,颜色就会看起来深一些了* @return*/private int colorBurn(int RGBValues) {int alpha = RGBValues >> 24;int red = RGBValues >> 16 & 0xFF;int green = RGBValues >> 8 & 0xFF;int blue = RGBValues & 0xFF;red = (int) Math.floor(red * (1 - 0.1));green = (int) Math.floor(green * (1 - 0.1));blue = (int) Math.floor(blue * (1 - 0.1));return Color.rgb(red, green, blue);}

这篇关于Android ToolBar+DrawerLayout的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147658

相关文章

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert

Python使用pip工具实现包自动更新的多种方法

《Python使用pip工具实现包自动更新的多种方法》本文深入探讨了使用Python的pip工具实现包自动更新的各种方法和技术,我们将从基础概念开始,逐步介绍手动更新方法、自动化脚本编写、结合CI/C... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

在Linux中改变echo输出颜色的实现方法

《在Linux中改变echo输出颜色的实现方法》在Linux系统的命令行环境下,为了使输出信息更加清晰、突出,便于用户快速识别和区分不同类型的信息,常常需要改变echo命令的输出颜色,所以本文给大家介... 目python录在linux中改变echo输出颜色的方法技术背景实现步骤使用ANSI转义码使用tpu

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文