[英]How to hide toolbar of activity when tablayout is present in fragment?
I have toolbar in activity and TabLayout in fragment and i want to hide toolbar of activity when RecyclerView items scroll up, Like this: 我在活动中有工具栏,在片段中有TabLayout,并且当RecyclerView项向上滚动时,我想隐藏活动的工具栏,像这样:
how to achieve this effect? 如何达到这个效果? I don't want to create separate toolbar for each fragment that shows this effect.
我不想为显示这种效果的每个片段创建单独的工具栏。
Thank you. 谢谢。
I have tried this but this isn't working: 我已经尝试过了,但这不起作用:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
...Code of coordinator layout...
>
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
...Code of Tab layout.../>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
Try changing your AppBarLayout to below one, 尝试将您的AppBarLayout更改为以下一项,
<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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorPrimary"
app:tabIndicatorHeight="2dip"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/white"
app:tabTextAppearance="?android:attr/textAppearanceMedium"
app:tabTextColor="@color/colorAccent" />
</android.support.design.widget.AppBarLayout>
First add these two librarys to your project. 首先将这两个库添加到您的项目中。
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
Then change the theme defiend in your Manifest to a "noActionbar" theme. 然后将清单中的主题防御更改为“ noActionbar”主题。
Now you can copy an paste this code to your layout and customize it as you like. 现在,您可以将此代码粘贴到您的布局中,并根据需要对其进行自定义。
<android.support.design.widget.CoordinatorLayout 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"
xmlns:fab="http://schemas.android.com/tools"
android:id="@+id/rootLayout"
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:id="@+id/appbarlayout"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|snap"
app:elevation="4dp">
<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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="scroll|enterAlways|snap">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/title"
android:text="Your App title"
android:textSize="25dp"
android:textColor="#ffffff"/>
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_scrollFlags="snap|enterAlways"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:background="#FFFFFF"
app:layout_scrollFlags="scroll|enterAlways"/>
This should do it 这应该做
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.