ToolBar与AppcompatAcitivity实现浸入式Statusbar效果

toolbar是android sdk API21新增的组件,下面是谷歌官方的介绍文档:

standard toolbar for use within application content.

A Toolbar is a generalization of action bars for use within application layouts. While an action bar is traditionally part of an Activity‘s opaque window decor controlled by the framework, a Toolbar may be placed at any arbitrary level of nesting within a view hierarchy. An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.

Toolbar supports a more focused feature set than ActionBar. From start to end, a toolbar may contain a combination of the following optional elements:

  • A navigation button. This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app‘s choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar. The navigation button is vertically aligned within the Toolbar‘s minimum height, if set.
  • A branded logo image. This may extend to the height of the bar and can be arbitrarily wide.
  • A title and subtitle. The title should be a signpost for the Toolbar‘s current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  • One or more custom views. The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view‘sToolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
  • An action menu. The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions. Action buttons are vertically aligned within the Toolbar‘s minimum height, if set.

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standardlayout is discouraged on API 21 devices and newer.


上面的介绍的重点是,toolbar是api21 新增的组件,其主要用来弥补actionbar带来的不足,其继承自viewgroup,自由的属性包括导航按钮(类似向上按钮),logo图片,标题和子标题,一个或者多个自定义view,菜单。

其中有一句介绍尤为介绍 An application may choose to designate a Toolbar as the action bar for an Activity using the setActionBar() method.我们可以在任何activity中使用toolbar作为actiobar,但是api21之前只能使用setSupportActionbar(),相应的主题也要设置为NoActionbar(不要怀疑就是这样干,没错儿),about toolbar的好,我会娓娓道来(么么哒)

具体介绍详见sdk/docs/reference/android/widget/Toolbar.html


下面介绍一下AppCompatAcitivity

官方介绍:You can add an ActionBar to your activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme toTheme.AppCompat or a similar theme.

其主要用来向低版本的sdk提供添加actionbar的功能,并且提供Theme.AppCompat(就是谷歌推荐的Theme.Material)这样的主题,保证低版本也能获得高版本的效果。

详细介绍见:yourSDKpath/sdk/docs/reference/android/support/v7/app/AppCompatActivity.html


谷歌在新出的22.1 library 还包含了如下的组件,他们最大的特点就是可以在组件之中使用android:theme:来控制组件的样式

AppCompatAutoCompleteTextView 
AppCompatButton 
AppCompatCheckBox 
AppCompatCheckedTextView 
AppCompatEditText 
AppCompatMultiAutoCompleteTextView 
AppCompatRadioButton 
AppCompatRatingBar 
AppCompatSpinner 
AppCompatTextView

在介绍了如此多的好东西之后,我们开始步入正题,如何使用toolbar 与AppCompatAcitivity实现浸入式Statusbar效果。

首先使用AndroidStudio构建project。

第二步添加依赖库:


选择open module settings》app》dependency,点击下方的加号自行选择    ‘com.android.support:appcompat-v7:22.1.1‘

第三步:修改系统自带主题

将原有的values /style修改为: