美文网首页
使用动画效果实现隐藏,显示ToolBar

使用动画效果实现隐藏,显示ToolBar

作者: 幻无名 | 来源:发表于2018-01-05 16:16 被阅读719次

一:布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.kirito.testa.MainActivity">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/toolbar"
        android:background="@color/colorPrimary"
        android:layout_height="?attr/actionBarSize">

        <LinearLayout
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="?attr/actionBarSize">

            <TextView
                android:text="ZhiHuNews"
                android:textColor="#f9f8ef"
                android:layout_gravity="left"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v7.widget.Toolbar>

    <Button
        android:id="@+id/btn_hide"
        android:text="hide"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_show"
        android:text="show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

二:在values/styles.xml,AppTheme里添加以去除原来的actionbar

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

三:为两个button添加监听事件,隐藏,显示ToolBar

记得加上
setSupportActionBar(toolbar);
hide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                curY = toolbar.getTranslationY();//获取当前的y轴位置
                height = toolbar.getHeight();
                ObjectAnimator oa = ObjectAnimator.ofFloat(toolbar,"translationY",curY,-height);
                oa.setDuration(1000);
                oa.start();
            }
        });

        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                curY = toolbar.getTranslationY();
                ObjectAnimator oa = ObjectAnimator.ofFloat(toolbar,"translationY",curY,0f);//toolbar原位置是0
                oa.setDuration(2000);
                oa.start();
            }
        });

四:后面发现这种方式隐藏的toolbar会在原处留下空白,所以这种方法不行,后来通过Google,发现隐藏toolbar的方法是使用多种控件的混合,CoordinatorLayout,AppBarLayout,Toolbar,NestedScrollView,具体实现如下:

Toolbar在activity中的设置与之前的相同,主要是布局文件
<?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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ToolBarStyle" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView //这里放入要滑动的view
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">//这句很重要

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:text="@string/content" />
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

在res/values/strings.xml里添加

    <string name="appbar_scrolling_view_behavior" translatable="false">android.support.design.widget.AppBarLayout$ScrollingViewBehavior</string>

五:若要进一步学习CoordinatorLayout,AppBarLayout,Toolbar,NestedScrollView的使用,可参考

相关文章

网友评论

      本文标题:使用动画效果实现隐藏,显示ToolBar

      本文链接:https://www.haomeiwen.com/subject/ptjsnxtx.html