4000 AndroidStudio Toolbar 滑动隐藏以及返回按钮点击事件
2016-06-15 17:55
537 查看
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”
界面描述图如下:
具体代码示例:
Toolbar 使用简单介绍:
在布局中 findViewById 找到控件,注意需要 v7 适配包中的Toolbar
getSupportActionBar(toolbar);
想使用 toolbar 的返回按钮(都要在setSupportActionBar 后调用)实现 Toolbar 点击返回事件:
或者
getSupportActionBar().setDisplayHomeAsUpEnable(true);
重写 onOptionsItemSelected
使用 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>
Toolbar 使用简单介绍:
在布局中 findViewById 找到控件,注意需要 v7 适配包中的Toolbar
getSupportActionBar(toolbar);
想使用 toolbar 的返回按钮(都要在setSupportActionBar 后调用)实现 Toolbar 点击返回事件:
toolbar.setNavigationIcon( mDrawable);//或者在布局中 app:navigationIcon="?attr/homeAsUpIndicator" 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); }
相关文章推荐
- 5、Android Service测试
- 5、Android Service测试
- 在Android Studio中有六种依赖
- Android-Universal-Image-Loader 源码解读
- Android 照相 滤镜
- Android下新增api编译问题
- Android IPC机制学习(Android艺术开发探索)
- Android 数据库(SQLite)的导入导出命令
- android 使用GreenDao
- RealtiveLayout(相对布局属性)
- 如何查看Android Studio 内存大小
- Android Studio常用快捷键
- android禁止ScrollView内的控件改变之后自动滚动
- Android 打开文件管理器,并返回选中文件的path
- android 键盘中如何加入一个Rupee货币的符号
- Android Button的Text如果是英文,不管大小写默认都会变成大写,改成原样输出
- android studio 如何调用自定义的framework.jar包在andorid.jar包之前呢
- Android 下陀螺仪、加速度和磁场传感器数据定义
- android项目智慧北京开发完整步骤:第二天
- Android下新增JNI过程中启动新增系统服务(addService)遇安全错误问题