1、TitleBar是標題欄
2、ActionBar:ActionBar位於Activity的頂部,可用來顯示activity的標題、Icon、Actions和一些用於交互的View。它也可被用於應用的導航。
注意:當狀態欄設置為透明時,ToolBar會與狀態欄重疊,這時需要在根布局下設置一個屬性:
android:fitsSystemWindows="true"
當ToolBar與頁面內容重疊時(如ListView等),在ListView下設置一個屬性:
android:layout_below="@+id/toolbar"
3、ToolBar是用來代替ActionBar的
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar=getSupportActionBar;
actionBar.setTitle("父標題");//父標題
actionBar.setSubTitle("子標題");//子標題
actionBar.setLogo(android.drawable.ic_launcher);//設置圖標
actionBar.setDisplayHomeAsUpEnabled(true); //設置返回箭頭顯示
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
4、StatusBar是頂部的狀態欄
5、AppBarLayout:AppBarLayout 是繼承LinerLayout實現的一個ViewGroup容器組件,它是為了Material Design設計的App Bar,支持手勢滑動操作。
默認的AppBarLayout是垂直方向的,它的作用是把AppBarLayout包裹的內容都作為AppBar。
此處將Toolbar 和Tablayout的組合部分共同構成 AppBar的效果。
注意: AppBarLayout必須作為Toolbar的父布局容器
AppBarLayout是支持手勢滑動效果的,不過的跟CoordinatorLayout配合使用
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"></android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll"
app:tabIndicatorColor="@android:color/holo_red_dark"
app:tabSelectedTextColor="@android:color/holo_red_dark"
app:tabTextColor="@android:color/black" />
</android.support.design.widget.AppBarLayout>
注意:
在Fragment中要想使Activity中的右上方菜單可顯示,需要在onCreate()方法中添加:
setHasOptionsMenu(true);//使右上方菜單可顯示要想清除菜單:
Menu menu = toolbar.getMenu(); menu.clear();同樣,完全清除菜單,也需要在Fragment中添加:setHasOptionsMenu(true);
6、CoordinatorLayout:CoordinatorLayout是一個增強型的FrameLayout。它的作用有兩個
(1)作為一個布局的根布局(2)為子視圖之間相互協調手勢效果的一個協調布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:="scroll|enterAlways" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill" />
</android.support.design.widget.AppBarLayout>
<!--可滑動的布局內容-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_discuss"
android:layout_gravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
從上面布局看到,CoordinatorLayout協調布局中包裹了兩個布局,一個是RecycleView,一個是AppBarLayout,以及FAB按鈕。
我們來看看CoordinatorLayout是怎么來協調這兩個子視圖手勢操作的。
1、由於CoordinatorLayout是FrameLayout布局,我們可以通過
android:layout_gravity="bottom|end"
屬性來控制組件在整個布局中的位置,比如上面效果中的FAB就是通過android:layout_gravity=”bottom|end”來確定 FAB的位置在底端的最右邊的位置。
2.為了達到上面效果圖的手勢動畫效果,我們必須做如下設置,通過app:layout_scrollFlags=”scroll|enterAlways” 屬性來確定哪個組件是可滑動
設置的layout_scrollFlags有如下幾種選項:
scroll: 所有想滾動出屏幕的view都需要設置這個flag- 沒有設置這個flag的view將被固定在屏幕頂部。enterAlways: 這個flag讓任意向下的滾動都會導致該view變為可見,啟用快速“返回模式”。enterAlwaysCollapsed: 當你的視圖已經設置minHeight屬性又使用此標志時,你的視圖只能已最小高度進入,只有當滾動視圖到達頂部時才擴大到完整高度。exitUntilCollapsed: 滾動退出屏幕,最后折疊在頂端。
我們上面的布局中 給Toolbar設置了app:layout_scrollFlags屬性,因此,Toolbar是可以滾動出屏幕,且向下滾動有可以出現。
3.為了使得Toolbar可以滑動,我們必須還得有個條件,就是CoordinatorLayout布局下包裹一個可以滑動的布局,比如 RecyclerView,NestedScrollView(經過測試,ListView,ScrollView不支持)具有滑動效果的組件。並且給這些組件設置如下屬性來告訴CoordinatorLayout,該組件是帶有滑動行為的組件,然后CoordinatorLayout在接受到滑動時會通知AppBarLayout 中可滑動的Toolbar可以滑出屏幕了。
app:layout_behavior="@string/appbar_scrolling_view_behavior"
總結: 為了使得Toolbar有滑動效果,必須做到如下三點:
- CoordinatorLayout必須作為整個布局的父布局容器。
- 給需要滑動的組件設置 app:layout_scrollFlags=”scroll|enterAlways” 屬性。
app:layout_behavior="@string/appbar_scrolling_view_behavior"
7、CollapsingToolbarLayout
8、NavigationView
9、去掉標題欄:
(1)代碼
//去掉標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
//隱藏狀態欄
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
注:requestWindowFeature的操作都要放在setContentView的前面
(2)在AndroidManifest.xml中進行修改
如果去掉單個的Activity:
<activity android:name=”.MainActivity” android:theme=”
如果我們要設置整個Application都去掉title bar,那么就設置application:
<application android:theme=”
(3)讓Activity不要繼承ActionBarActivity
注:android:theme="@android:style/Theme.NoTitleBar"只是去掉標題欄
android:theme="@android:style/Theme.NoTitleBar.FullScreen"是去掉標題欄和狀態欄