Android - 移除Toolbar和TabLayout之间的阴影

11

我正在尝试使用CollapsingToolbarLayout 创建布局。但有一件事我不明白,我想要移除工具栏和选项卡布局之间的阴影。我已经尝试了几种方法,但没有成功移除阴影。请问有人可以帮助我吗?谢谢。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/adview"
    android:fitsSystemWindows="true">

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="50dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        android:translationZ="2dp"
        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="3dp"
        app:tabMode="fixed"
        app:tabTextColor="@color/tabs_text_selector" />

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

        <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="20dp"
            app:expandedTitleMarginStart="20dp"
            app:expandedTitleTextAppearance="@style/detalle_txt_expanded"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/detalle_img"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                android:src="@drawable/img_thumb_m"
                android:transitionName="@string/transition"
                app:layout_collapseMode="parallax"
                tools:targetApi="lollipop" />

            <ImageView
                android:id="@+id/detalle_img_tipo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/img_edificio"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:minHeight="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme"/>

        </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/detalle_info_fab_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:clickable="false"
        android:src="@drawable/ic_check1"
        app:fabSize="mini"
        app:backgroundTint="@android:color/white"
        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom|right|end" />

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

<include
    android:id="@+id/adview"
    layout="@layout/adview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" />

4个回答

20

尝试将AppBarLayout的属性设置为app:elevation="0dp"(而不是android:elevation)。

如果您的xml中没有应用程序命名空间,请添加xmlns:app="http://schemas.android.com/apk/res-auto"

这两个属性之间的区别可以在这里找到。

之后,检查是否使用任何这些属性添加了一些背景/边框:

 <android.support.design.widget.TabLayout
    android:background="?attr/colorPrimary"
    android:translationZ="2dp"
    app:layout_anchor="@+id/appbar"
    app:layout_anchorGravity="bottom"
    app:tabGravity="fill"
    app:tabIndicatorColor="@android:color/white"/>

或者是你为AppBarLayout使用的主题。


1
你的代码只能工作一半。现在,在工具栏和选项卡布局之间显示了一条线(不是阴影)。 - IMS
@IMS 编辑了答案。请检查您为这些视图设置的颜色和主题。 - RominaV
@RominaV,对我不起作用。请帮忙看一下:https://stackoverflow.com/questions/63658694/why-does-toolbar-shadow-not-removing-in-android - user13327428

6
<android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            app:titleTextColor="@android:color/white" />

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

<include layout="@layout/content_tablayout" />

使用app:elevation="0dp"来移除阴影。对我来说一直都有效。希望对你也有帮助。

另外,也可以通过编程方式实现。

appBar.setOutlineProvider(null);

2
你必须将工具栏(Toolbar)和选项卡布局(TabLayout)放置在同一个AppBarLayout中。
    <android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="20dp"
        app:expandedTitleMarginStart="20dp"
        app:expandedTitleTextAppearance="@style/detalle_txt_expanded"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/detalle_img"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/img_thumb_m"
            android:transitionName="@string/transition"
            app:layout_collapseMode="parallax"
            tools:targetApi="lollipop" />

        <ImageView
            android:id="@+id/detalle_img_tipo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/img_edificio"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:minHeight="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme"/>

    </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        android:translationZ="2dp"
        app:layout_anchor="@+id/appbar"
        app:layout_anchorGravity="bottom"
        app:tabGravity="fill"
        app:tabIndicatorColor="@android:color/white"
        app:tabIndicatorHeight="3dp"
        app:tabMode="fixed"
        app:tabTextColor="@color/tabs_text_selector" />

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

看一下这个问题


0

我知道这个问题已经有答案了。但是我想分享一些我通过艰难的方式学到的东西,以便面对相同问题的人可以节省时间。

你需要将 AppBarLayout 包装在 CoordinatorLayout 中,否则 app:elevation 将无法工作。

工作示例:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接