工具栏在 TabLayout 上投下阴影

Toobar drops shadow on TabLayout

everybody! I have a fragment in FrameLayout container within activity, which has Navigation Drawer. Fragment has TabLayout. And the problem is, as you can see, that activity's toolbar drops shadow on fragment's TabLayout. I dont want to place TabLayout in activity's AppBar layout because I won't have access to it from fragment and, moreover, I don't need that TabLayout anywhere else.

This is my 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" />

And this is 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>

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

I also want the Toolbar to hide when I scroll up list in fragment within ViewPager

Do you have any ideas how can I achive that?

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

用这个。在我的项目上工作。

ViewCompat.setElevation(mAppBarLayout, 0);

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

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

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

改为尝试在 StateListAnimator 中将海拔设置为 0,如下所示 Build.VERSION.SDK_INT >= 21

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

来自支持库 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);