【问题标题】:How to hide toolbar of activity when tablayout is present in fragment?片段中存在tablayout时如何隐藏活动工具栏?
【发布时间】:2016-02-24 07:37:06
【问题描述】:

我在活动中有工具栏,在片段中有 TabLayout,我想在 RecyclerView 项目向上滚动时隐藏活动的工具栏,像这样:

如何实现这个效果?我不想为显示这种效果的每个片段创建单独的工具栏。

谢谢。

我已经试过了,但这不起作用:

<?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>

【问题讨论】:

标签: java android android-fragments material-design android-coordinatorlayout


【解决方案1】:

尝试将您的 AppBarLayout 更改为低于 1,

<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>

【讨论】:

  • 我已经知道了,但是我有工具栏在活动中。我想通过向上滚动视图寻呼机而不在片段中创建新工具栏来向上滚动活动工具栏。
【解决方案2】:

首先将这两个库添加到您的项目中。

compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'

然后将 Manifest 中的主题定义更改为“noActionbar”主题。

现在您可以将此代码复制并粘贴到您的布局中,并根据需要对其进行自定义。

<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"/>

应该这样做

【讨论】:

  • 我已经知道了,但是我有工具栏在活动中。我想通过向上滚动视图寻呼机而不在片段中创建新工具栏来向上滚动活动工具栏。
  • 据我所知这是不可能的。
  • 这段代码真的救了我一天!我在折叠栏布局时遇到了问题,但这对我帮助很大!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-21
  • 1970-01-01
  • 2022-10-17
  • 1970-01-01
  • 2016-05-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多