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

相关文章

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并