Android Toolbar使用系统原生返回键,并改变其颜色,自定义图片替换系统原生返回键

来源:互联网 发布:什么营销软件最好 编辑:程序博客网 时间:2024/05/22 01:51

在2014年Google I/O大会上发布的material design中提到了Toolbar这个用来取代Actionbar的概念,关于Toolbar的文章有很多,我在这里简单说一说关于Toolbar系统原生返回键,权作狗尾续貂吧。

首先是我的布局文件:一个AppBarLayout里面套个Toolbar

[html] view plain copy
  1. <android.support.design.widget.AppBarLayout  
  2.         android:id="@+id/about_appbar"  
  3.         android:layout_width="match_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:theme="@style/toolbar_theme">  
  6.         <android.support.v7.widget.Toolbar  
  7.             android:id="@+id/about_toolbar"  
  8.             android:layout_width="match_parent"  
  9.             android:layout_height="?attr/actionBarSize"  
  10.             android:background="#ffffff">  
  11.             <TextView  
  12.                 android:layout_width="wrap_content"  
  13.                 android:layout_height="wrap_content"  
  14.                 android:layout_gravity="center_horizontal"  
  15.                 android:text="About"  
  16.                 android:textColor="#000000"  
  17.                 android:textSize="18sp"/>  
  18.         </android.support.v7.widget.Toolbar>  
  19.     </android.support.design.widget.AppBarLayout>  
这里AppBarLayout中的属性theme就是用来控制我的返回键的颜色的。

下面是我在style.xml文件中定义的属性。

[html] view plain copy
  1. <style name="toolbar_blue_theme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">  
  2.         <item name="colorControlNormal">@color/color_text_blue</item>  
  3.         <item name="android:actionBarSize">46dp</item>  
  4. </style>  


colorControlNormal就是我们指定的返回键的颜色,Android:actionBarSize 是我们可以另指定的Toolbar高度


当我的AppBarLayout中使用toolbar_theme时,
当换成toolbar_blue_theme时,

当然,光写布局文件还不够,相关代码贴上:

[java] view plain copy
  1. @Bind(R.id.about_toolbar)  
  2.     Toolbar mToolbar;  
  3.   
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.activity_about);  
  8.   
  9.         ButterKnife.bind(this);  
  10.         setSupportActionBar(mToolbar);  
  11.         ActionBar actionBar =  getSupportActionBar();  
  12.         if(actionBar != null) {  
  13.             actionBar.setDisplayHomeAsUpEnabled(true);  
  14.             actionBar.setDisplayShowTitleEnabled(false);  
  15.         }  
  16.     }  
  17.   
  18.     @Override  
  19.     public boolean onOptionsItemSelected(MenuItem item) {  
  20. <span style="white-space:pre">    </span>//点击back键finish当前activity  
  21.         switch (item.getItemId()) {  
  22.             case android.R.id.home:  
  23.                 finish();  
  24.                 break;  
  25.         }  
  26.         return true;  
  27.     }  

当需要设定与自己App风格一致的back键时,只要改变style中的颜色就可以啦。当然,可能你的UI设计师会给你切好的图,这时候怎么办呢,怎么用设计师切好的图替换我们系统原生的返回键呢?方法有两个。

1、在我们引入的Appbar的theme中添加一个Item,将设计师给我们的图放进去

[html] view plain copy
  1. <item name="android:homeAsUpIndicator">@drawable/web_detail_back</item>  
2、在我们的Toolbar中添加属性

[html] view plain copy
  1. app:navigationIcon="@drawable/web_detail_back"  
添加这个属性需要我们在根布局中添加(AS会有提示的)

[html] view plain copy
  1. xmlns:app="http://schemas.android.com/apk/res-auto"  

最终的效果就是这样的了

阅读全文
0 0
原创粉丝点击