如何像在Whatsapp中一样隐藏动作栏?

时间:2015-10-02 07:45:29

标签: android android-toolbar android-actionbar-compat android-coordinatorlayout

我有一个包含以下布局的片段,我在视图寻呼机的帮助下添加了两个片段:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:background="#2096f3"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"
    />

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

在代码中:

    TabLayout tabLayout = (TabLayout) rootView.findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText(tabs[0]));
    tabLayout.addTab(tabLayout.newTab().setText(tabs[1]));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) rootView.findViewById(R.id.pager);

    adapter = new MyViewPagerAdapter(getFragmentManager(),2);
    viewPager.setAdapter(adapter);

我在视图寻呼机的片段中有一个滚动视图。当我开始向下滚动时,我希望操作栏隐藏。怎么做到这一点?

我在活动中有一个工具栏,我正在添加一些片段。如何调整它得到预期的结果:

工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/background_floating_material_dark"
android:elevation="4dp"
android:paddingTop="1dp"
app:layout_scrollFlags="scroll|enterAlways">

3 个答案:

答案 0 :(得分:5)

查看this library及其示例。

gif动画:

image.gif

示例文件为“ViewPagerTabScrollViewActivity

答案 1 :(得分:1)

您可以使用自定义样式:

<resources>
 <style name = "AppTheme" parent = "android:Theme.Holo.Light.DarkActionBar"></style>
 <style name = "HiddenActionBar" parent = "@android:style/Theme.Holo.Light">
  <item name = "android:windowActionBar">false</item>
  <item name = "android:windowNoTitle">true</item>
 </style>

</resources>

并在AndroidManifest.xml上使用它:

<application
 android:icon = "@drawable/ic_launcher"
 android:label = "@string/app_name"
 android:theme = "@style/HiddenActionBar">

答案 2 :(得分:0)

此XML代码适合我:

<android.support.design.widget.CoordinatorLayout
   android:id="@+id/coordinatorLayout"
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:elevation="0dp"
    android:id="@+id/appBarLayout"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="0dp"
        android:id="@+id/tabanim_toolbar"
        app:layout_scrollFlags="scroll|enterAlways"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#2096f3"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>

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

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/view_pager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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