简体   繁体   English

Android - 去除工具栏和 TabLayout 之间的阴影

[英]Android - Remove shadow between Toolbar and TabLayout

I'm trying to make a layout with CollapsingToolbarLayout .我正在尝试使用CollapsingToolbarLayout进行布局。 But I do not get one thing.但我没有得到一件事。 I want to remove the shadow between Toolbar and TabLayout .我想删除 Toolbar 和 TabLayout 之间的阴影 I've tried several ways and I have not managed to remove the shadow.我尝试了几种方法,但没有设法消除阴影。 Can anybody help me?有人可以帮我吗? Thank you谢谢

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

Try setting app:elevation="0dp" (not android:elevation ) for your AppBarLayout .尝试为您的AppBarLayout设置app:elevation="0dp" (不是android:elevation )。

In case you don't have the app namespace in your xml, add xmlns:app="http://schemas.android.com/apk/res-auto" .如果您的 xml 中没有 app 命名空间,请添加xmlns:app="http://schemas.android.com/apk/res-auto"

The difference between these two properties can be found here .可以在此处找到这两个属性之间的区别。

After that, check that you're not adding some background/border with any of these properties:之后,检查您是否没有使用以下任何属性添加一些背景/边框:

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

Or the theme you're using for the AppBarLayout .或者您用于AppBarLayout的主题。

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

Use app:elevation="0dp" to remove the shadow.使用app:elevation="0dp"去除阴影。 It has always worked for me.它一直对我有用。 Hope it works for you.希望它对你有用。

And also by programatically,并且还以编程方式,

appBar.setOutlineProvider(null);

You have to put the Toolbar and the TabLayout in the same AppBarLayout你必须把 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>

Take a look at this issue看看这个问题

I know the question is already answered.我知道这个问题已经回答了。 But I want to share something that I learnt the hard way so that people who are facing the same problems can save time.但我想分享一些我通过艰难方式学到的东西,以便面临同样问题的人可以节省时间。

You need to wrap AppBarLayout inside CoordinatorLayout or else app:elevation will not work.您需要将AppBarLayout包装在CoordinatorLayout ,否则app:elevation将不起作用。

Working example:工作示例:

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM