TOOLBAR和沉浸式状态栏入门
来源:互联网 发布:淘宝店铺折扣价设置 编辑:程序博客网 时间:2024/05/21 06:56
toolbar是android sdk API21新增的组件,是ActionBar的加强版,更加方便自定义布局。api21之后可以直接使用toolbar,但是我们肯定要支持api21之前的,下边的所有代码都是兼容21之前的
简单toolbar
步骤
首先写好style
<resources> <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="windowActionBar">false</item> <item name="android:windowNoTitle">true</item> </style></resources>
styles.xml(v19),写这个是为了状态栏,因为19对应android4.4,只有这个版本以上设置了下边的style才能改变状态栏颜色
<resources> <style name="ActionBarTheme" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowTranslucentNavigation" >true</item> <item name="android:windowTranslucentStatus">true</item> </style></resources>
指定activity的theme为android:theme="@style/ActionBarTheme"
toolbar的xml代码
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffff00" android:layoutMode="opticalBounds" android:minHeight="?attr/actionBarSize" android:popupTheme="@style/ThemeOverlay.AppCompat.Dark" android:theme="@style/ThemeOverlay.AppCompat.Dark"></android.support.v7.widget.Toolbar>
布局
<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:fitsSystemWindows="true" android:orientation="vertical" tools:context=".MainActivity"> <include layout="@layout/toolbar"></include> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></LinearLayout>
注意android:fitsSystemWindows="true"这句话,防止toolbar和status bar有重叠
主要代码
public class MainActivity extends AppCompatActivity { private Toolbar mToolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);// 设定状态栏的颜色,当版本大于4.4时起作用 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(android.R.color.holo_red_light); } mToolbar = (Toolbar) findViewById(R.id.toolbar); mToolbar.setTitle("mainTitle");// 标题的文字需在setSupportActionBar之前,不然会无效 mToolbar.setTitleTextColor(Color.parseColor("#ff0000")); setSupportActionBar(mToolbar); mToolbar.setNavigationIcon(android.R.drawable.ic_delete); mToolbar.setLogo(android.R.drawable.ic_media_play); }}
效果如上,toolbar上从左到右依次为导航图,logo,标题
对应module:toolbar0
自定义布局toolbar
上文的toolbar只有导航图,logo,标题,我能不能加一些自定义布局呢,当然可以,就在toolbar对应xml的布局里加就可以了。
比如我们再toolbar.xml内部加一个LinearLayout,代码如下
<?xml version="1.0" encoding="utf-8"?><android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffff00" android:layoutMode="opticalBounds" android:minHeight="?attr/actionBarSize" android:popupTheme="@style/ThemeOverlay.AppCompat.Dark" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff0000" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="11111" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="2222" /> </LinearLayout></android.support.v7.widget.Toolbar>
那么效果变为
此时需要注意
toolbar的android:background="#ff0000"有时候会无效
只要toolbar里写了自定义布局,比如里面写了个LinearLayout,那么toolbar的android:background="#ff0000",这句话就会出现以下情况,如果compileSdk是22,那么android:background="#ff0000"这句话有效果,如果compileSdk是21,那么android:background="#ff0000"这句话没有效果,原因大概是compile'com.android.support:appcompat-v7:22.1.1',这个jar包要求compilesdk在22或以上。如果toolbar里没有写自定义布局,那么这句话肯定有效
对应module:toolbar1
V7的actionbar就是toolbar实现的
Toolbar在Appcombat V7库是一个公开的控件,位于android.support.v7.widget包中,继承自ViewGroup,所以我们可以像使用普通的容器控件一样使用Toolbar,并主动调用相关方法实现ActionBar的功能。但是,Google是希望在Appcombat V7库中使用Toolbar来代替现有ActionBar的实现,所以肯定不会仅仅提供个控件就好了,Appcombat V7还把Toolbar封装进默认的Activity中。在新建工程时,创建向导会默认导入Appcombat V7库,自动创建的Activity也会继承自对Toolbar封装过的ActionBarActivity,创建工程后立刻运行应用,此时已是Toolbar实现了ActionBar的功能。我们可以试一下,下边代码获得的v,其实就是Toolbar类型的
View v=findViewById(R.id.action_bar);
沉浸式的效果
再来看一个沉浸式的效果,把toolbar和状态栏的颜色调为一致android.R.color.holo_blue_bright
状态栏颜色
4.4以上可以修改状态栏颜色,其实控制状态栏颜色很简单
首先在values-v19文件夹内的styles.xml内添加一个activity的style,这个style内部必须填<itemname="android:windowTranslucentStatus">true</item>
可以是
<style name="MyTheme"parent="Theme.AppCompat.Light.DarkActionBar"> <itemname="android:windowTranslucentStatus">true</item></style>
也可以是
<style name="MyTheme"parent="Theme.AppCompat.NoActionBar"> <itemname="android:windowTranslucentStatus">true</item> </style>
parent是无所谓的,大部分都可以,关键是android:windowTranslucentStatus要置为true
然后制定activity使用此style,最后在oncreate里面添加代码
// 设定状态栏的颜色,当版本大于4.4时起作用
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { SystemBarTintManager tintManager = new SystemBarTintManager(this); tintManager.setStatusBarTintEnabled(true); tintManager.setStatusBarTintResource(android.R.color.holo_red_light); }
就可以在4.4级以上版本设置状态栏颜色了
注意状态栏颜色设置其实和toolbar人没有一毛钱关系
SystemBarTintManager可以使用setStatusBarTintDrawable,虽然这个函数已经被废弃,但是不要和别人共享Drawable对象,我曾经让statusbar和actionbar共享一个背景的drawable,会出问题,这个函数会失效,具体原因不明,但是单独使用一个Drawable是没问题的
参考文献
http://www.bkjia.com/Androidjc/993003.html
http://www.cnblogs.com/tianzhijiexian/p/4081562.html
http://blog.csdn.net/qq284565035/article/details/47102197
http://blog.csdn.net/hello__zero/article/details/30517245
- TOOLBAR和沉浸式状态栏入门
- Android之ToolBar和自定义ToolBar实现沉浸式状态栏
- Toolbar实现沉浸式状态栏
- toolbar实现沉浸式状态栏
- ToolBar沉浸式状态栏的兼容实现
- 沉浸式状态栏之Toolbar解决方案
- Android,沉浸式状态栏,状态栏以及Toolbar颜色分开设置
- 画廊etiennelawlor imagegallery真正全屏,使用隐藏状态栏和沉浸式浮动Toolbar
- DrawerLayout侧滑菜单、Toolbar和沉浸式状态栏的使用
- 透明状态栏和沉浸式
- 漫谈透明状态栏和沉浸式状态栏
- 优雅地实现 Android Toolbar 及沉浸式状态栏
- ToolBar、沉浸式状态栏的应用(简单粗暴)
- Android DrawerLayout+ToolBar+NavigationView实现策划效果,沉浸式状态栏
- Toolbar两步实现沉浸式系统状态栏
- 状态栏、沉浸式状态栏
- android沉浸式状态栏和虚拟按键
- Android 沉浸式状态栏和侧滑栏--详解
- iOS之用NSTimer定时刷新按钮的文字,避免按钮闪烁的办法
- 简单的Android笔记sharePreferences和Editor
- UVa12097 - Pie
- Qt5基本对话框
- 文件下载实例
- TOOLBAR和沉浸式状态栏入门
- Ubuntu中crosstool-ng编译出错:[ERROR] gtype-desc.c:8829:18: error: subscripted value is neither array nor
- JavaScript 闭包详解
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 解决方法
- 数据库原理----学习目标
- 列表小结
- Lucene的几种分词系统
- RAC Wait Event: gcs log flush sync
- 55分钟学会正则表达式(译)