【问题标题】:Toobar drops shadow on TabLayout工具栏在 TabLayout 上投下阴影
【发布时间】:2016-08-08 20:30:10
【问题描述】:

大家!我在活动中的 FrameLayout 容器中有一个片段,它具有导航抽屉。片段有 TabLayout。如您所见,问题是 活动的工具栏在片段的 TabLayout 上投下阴影。我不想将 TabLayout 放在活动的 AppBar 布局中,因为我无法从片段访问它,而且,我在其他任何地方都不需要该 TabLayout。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/mainNavigationView"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

这是app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

    <FrameLayout
        android:id="@+id/mainFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

这是fragment_by_day_schedule.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

 <android.support.design.widget.TabLayout
     android:id="@+id/byDayTabLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:tabBackground="?attr/colorPrimary"
     app:tabMode="scrollable" />

<android.support.v4.view.ViewPager
    android:id="@+id/byDayViewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

当我在 ViewPager 的片段中向上滚动列表时,我还希望工具栏隐藏

您有什么想法可以实现吗?

【问题讨论】:

  • 是什么让你认为如果你把它放在工具栏里,你将无法从片段中访问标签布局?老实说,我认为在工具栏内包含选项卡是一种更好且更广泛理解的模式。也可以实现在viewpager上移时隐藏工具栏的效果。
  • 再次检查我的答案。我更新了你的第二个问题。

标签: android android-fragments material-design android-toolbar android-tablayout


【解决方案1】:

使用这个。适用于我的项目。

ViewCompat.setElevation(mAppBarLayout, 0);

你的第二个问题是如何隐藏你的工具栏? 将您的父布局线性布局更改为布局。

<android.support.design.widget.CoordinatorLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

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

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

    <FrameLayout
        android:id="@+id/mainFragmentContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>

在此之后你必须告诉 android 工具栏什么时候应该隐藏。你应该告诉哪个视图可以滚动。您已将此 app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到您的 ViewPager 中。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

 <android.support.design.widget.TabLayout
     android:id="@+id/byDayTabLayout"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     app:tabBackground="?attr/colorPrimary"
     app:tabMode="scrollable" />

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

【讨论】:

  • 从来不知道 ViewCompat.setElevation()
【解决方案2】:

支持库 v24.0.0 中的 AppBarLayout 使用StateListAnimator。如果您在这个新版本中使用,设置appBayLayout.setElevation(0) 将无效。

尝试在StateListAnimator 中将海拔设置为0,如下Build.VERSION.SDK_INT &gt;= 21

StateListAnimator stateListAnimator = new StateListAnimator();
stateListAnimator.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
appBarLayout.setStateListAnimator(stateListAnimator);

【讨论】:

    【解决方案3】:

    来自支持库 v24.0.0 AppBarLayout 使用 StateListAnimator 设置高度。要获得与 24.0.0 之前相同的行为,只需将 animator 设置为 null 并调用 setElevation():

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        appbarLayout.setStateListAnimator(null);
    }
    
    ViewCompat.setElevation(appbarLayout, 0);
    

    【讨论】:

      【解决方案4】:

      您需要确保所有东西都具有相同的高度。

      【讨论】:

      • 我不想使用elevation,因为它不适用于pre-lollipop
      • 我不确定这是一个想/不想的场景...阴影是从高程绘制的。如果您不想要阴影,则应指定高度为 0。不过,这会使您的设计变平并与使用 Surfaces 的 Material Design 语言模式相矛盾。
      • 刚刚在 pre-lollipop 上尝试过,但在 API 21+ 上不起作用。没有效果(
      猜你喜欢
      • 2016-03-08
      • 1970-01-01
      • 2016-04-25
      • 2016-10-27
      • 2017-07-13
      • 2012-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      相关资源
      最近更新 更多