android5.0之toolBar

  toolBar是android5.0之后提出的,可以简单的理解为ActionBar的升级版吧,在5.0之后,官方推荐使用ToolBar!下面开启ToolBar的正文(老样子,有问题请指正,有疑问,偶们共同讨论!嘎嘎......)

  ①toolBar是5.0之后提出的,那么低版本的该怎么办啊?老规矩咯,引入V7包'com.android.support:appcompat-v7:22.1.1',就是介个!(注意:下面的一些类都是V7包里的,不要写错了!!!)

  ②V7包引入之后,还是不可以直接使用toolBar,而是在styles.xml文件下,指定如下的关系:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

  换而言之呢,就是ToolBar和ActionBar是不能共存的,使用ToolBar就必须将ActionBar除去,所以也就必须有上面的指定。

  ③现在可以在布局文件中使用ToolBar了,但是为了兼容,必须是V7包中的。指定如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
3     android:id="@+id/toolBar"
4     android:layout_width="match_parent"
5     android:layout_height="wrap_content"
6     android:background="#ff88ff43"
7     android:orientation="vertical">
8 
9 </android.support.v7.widget.Toolbar>

  ④在MainActivity.java中,找到toolBar对应的id,然后设置toolBar(V7包),需要注意的是,此时MainActivity继承的必须是V7包中的ActionBarActivity,关键代码如下:

1  private void initView() {
2         setContentView(R.layout.activity_main);
3         toolbar = (Toolbar) findViewById(R.id.toolBar);
4         //Set a to act as the for this Activity window.
5         setSupportActionBar(toolbar);
6     }

  toolBar就搞定了!很简单吧!只要注意那几个点就完全没有问题!

  代码下载:https://github.com/SamSarah1/Android-Demo

 

posted @ 2015-11-09 22:23  SamSarah  阅读(263)  评论(0编辑  收藏  举报