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"是去掉标题栏和状态栏