Android工具栏隐藏功能无效

时间:2015-11-06 15:16:08

标签: android android-toolbar

当我在ListView Fragment中滚动ToolBar时,我<?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:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/home_appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <include layout="@layout/toolbar_layout"/> <android.support.design.widget.TabLayout android:id="@+id/home_tabs" android:layout_width="match_parent" android:layout_height="wrap_content"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/home_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> </android.support.design.widget.CoordinatorLayout> 没有隐藏/显示。 我使用了来自here的样本 这是我的 xml

Fragment

这是public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home, container, false); mViewPager = (ViewPager) view.findViewById(R.id.home_viewpager); mAdapter = new HomeScreenPagerAdapter(getChildFragmentManager(), getActivity()); mViewPager.setAdapter(mAdapter); mTabLayout = (TabLayout) view.findViewById(R.id.home_tabs); mTabLayout.setupWithViewPager(mViewPager); return view; } 中的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|enterAlways|snap"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

添加工具栏布局

ToolBar

我可以运行此代码,但{{1}}并不隐藏。我该如何隐藏/显示它?

3 个答案:

答案 0 :(得分:2)

您的工具栏实施没有任何问题。

appbar_scrolling_view_behavior无效的原因是片段中的布局中存在不受支持的小部件。尝试将您的布局封装在NestedScrollView中,例如

 <android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ScrollView01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:context=".MainActivity">
        /* Your views */
    </RelativeLayout>
</android.support.v4.widget.NestedScrollView>

答案 1 :(得分:1)

您应该向我们展示toolbar_layout的代码。无论如何,您应该像这样添加toolbar layout_scrollFlags

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

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

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

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

答案 2 :(得分:0)

我在可滚动布局中通过NestedScrollView而不是ScrollView解决了同样的问题。

相关问题