AndroidStudio Toolbar 滑动隐藏以及返回按钮点击事件

来源:互联网 发布:淘宝卖家中心怎么找 编辑:程序博客网 时间:2024/06/06 15:03
 

AndroidStudio Toolbar 滑动隐藏以及返回按钮点击事件

标签: android
 4669人阅读 评论(0) 收藏 举报
 分类:

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);//或者在布局中 app:navigationIcon="?attr/homeAsUpIndicator"toolbar.setNavigationOnClickListener(new View.OnClickListener(){    public void onClick(View view){        finish();    }};
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

或者 
getSupportActionBar().setDisplayHomeAsUpEnable(true);

重写 onOptionsItemSelected

@Overridepublic boolean onOptionsItemSelected(MenuItem item) {    if(item.getItemId()==android.R.id.home){        finish();        return true;    }    return super.onOptionsItemSelected(item);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
阅读全文
0 0
原创粉丝点击