Android Toolbar 实现收缩展开动画:
使用 CoordinatorLayout 作为做外层布局,
ToolBar 使用 AppBarLayout和 CollapsingToolbarLayout 两个嵌套。
CollapsingToolbarLayout 需指定 layout_scrollFlags :scroll,exitUntilCollapsed,enterAlwaysCollapsed,|enterAlways,snap 其中的一种或多种。
CoordinatorLayout 里面的子布局需要添加可滑动的布局,如NestedScrollView或者RecycleView等,其他滑动如listview貌似不可以实现toolbar下滑收缩,可嵌套NestedScrollView,不过需解决滑动冲突问题,或直接使用RecycleView.
可滑动控件,如NestedScrollView,需添加动作标识:layout_behavior:@string/appbar_scrolling_view_behavior
使用 NestedScrollView 要添加 android:fillViewport=”true” 使子控件充满布局
为了状态栏和标题栏分开,需添加:android:fitsSystemWindows=”true”
界面描述图如下:

具体代码示例:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.zhou.picassotest.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center"
android:numColumns="4"
android:scrollbars="none"
android:stretchMode="columnWidth" />
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
Toolbar 使用简单介绍:
在布局中 findViewById 找到控件,注意需要 v7 适配包中的Toolbar
getSupportActionBar(toolbar);
想使用 toolbar 的返回按钮(都要在setSupportActionBar 后调用)实现 Toolbar 点击返回事件:
toolbar.setNavigationIcon( mDrawable);
toolbar.setNavigationOnClickListener(new View.OnClickListener(){
public void onClick(View view){
finish();
}
};
或者
getSupportActionBar().setDisplayHomeAsUpEnable(true);
重写 onOptionsItemSelected
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home){
finish();
return true;
}
return super.onOptionsItemSelected(item);
}