简体   繁体   English

Android 折叠工具栏删除高程和阴影

[英]Android Collapsing Toolbar remove elevation and shadow

I am trying to make a layout that has a gradient into the collapsing app bar which is a solid color.我正在尝试使布局具有渐变到折叠的应用程序栏,这是纯色。 This should be accomplishable through app:elevation="0dp" but that has not worked.这应该可以通过app:elevation="0dp"来完成,但这并没有奏效。 However, when I run 'Instant Run' on my app, instead of running a full build, I get the desired result.但是,当我在我的应用程序上运行“即时运行”时,我得到了想要的结果,而不是运行完整的构建。 Let me include pictures:让我附上图片:

Here is the current look: UI with the drop shadow, undesired result这是当前的外观:带有阴影的 UI,不想要的结果

在此处输入图片说明

Here is the desired look/what appears when I run instant run: UI without drop shadow, desired result这是我运行即时运行时所需的外观/出现的内容: UI 没有阴影,所需的结果

在此处输入图片说明

Here is my layout file:这是我的布局文件:

<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
android:windowActionBar="false"
android:id="@+id/main_background">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme"
    android:elevation="0dp"
    app:elevation="0dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:toolbarId="@+id/toolbar"
        android:elevation="0dp"
        app:elevation="0dp">

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

        </android.support.v7.widget.Toolbar>

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

</recycler>

In case this is unclear, I am referring to the drop shadow underneath the app_bar that is labeled "Reee".如果不清楚,我指的是 app_bar 下方标有“Reee”的阴影。 In the first picture, there is a shadow, in the second picture there is not a shadow.第一张图有阴影,第二张图没有阴影。 I do not want to have the shadow.我不想有阴影。 However, I can only achieve this look when I run instant run, then it goes away on next run / full compilation.但是,我只能在运行即时运行时实现这种外观,然后在下一次运行/完整编译时它就会消失。

I'm still looking for a fix for this issues and am quite frustrated that I have not found anything yet.我仍在寻找解决此问题的方法,并且对我还没有找到任何东西感到非常沮丧。 I have tried examples for setting stateListAnimator=@null to no avail.我已经尝试过将stateListAnimator=@null设置stateListAnimator=@null示例。

Alright doing some further research, I have a SwipeRefreshLayout with a Recyclerview underneath this appbar.好吧,做一些进一步的研究,我有一个 SwipeRefreshLayout,在这个应用栏下面有一个 Recyclerview。 When I remove the RecyclerView, I get the intended affect.当我删除 RecyclerView 时,我得到了预期的效果。 What could my RecyclerView be doing, and how can I fix it?我的 RecyclerView 可以做什么,我该如何解决?

Just set刚设置

android:stateListAnimator="@null"

or

app:elevation="0dp"

NOTE: you should use app: namespace NOT android:注意:你应该使用app:命名空间而不是android:

to your AppBarLayout到你的 AppBarLayout

You can do this by setting a custom stateListAnimator :您可以通过设置自定义stateListAnimator来做到这stateListAnimator

create a new animator "appbar_not_elevated.xml" in animator under res .创建一个新的动画“appbar_not_elevated.xml” animatorres

<item app:state_collapsible="true" app:state_collapsed="true">
    <objectAnimator android:propertyName="elevation"
                    android:valueTo="0"
                    android:valueType="floatType"
                    android:duration="1"/>
</item>

<item>
    <objectAnimator android:propertyName="elevation"
                    android:valueTo="0"
                    android:valueType="floatType"
                    android:duration="1"/>
</item>

This will set the elevation of collapsed as well as expanded state elevation to zero.这会将折叠和展开状态的高程设置为零。

Then set the stateListAnimator to AppBarLayout :然后将stateListAnimator设置为AppBarLayout

android:stateListAnimator="@animator/appbar_not_elevated"

Depending on the support library used, if you read the source code to set app bar elevation, you'll read:根据所使用的支持库,如果您阅读源代码来设置应用栏高度,您将阅读:

/**
 * @deprecated target elevation is now deprecated. AppBarLayout's elevation is now
 * controlled via a {@link android.animation.StateListAnimator}. If a target
 * elevation is set, either by this method or the {@code app:elevation} attribute,
 * a new state list animator is created which uses the given {@code elevation} value.
 *
 * @attr ref android.support.design.R.styleable#AppBarLayout_elevation
 */

which tells you, either you use app:elevation or a state list animator.它告诉你,你要么使用app:elevation要么使用状态列表动画师。 For the second option, add an xml selector into the folder animator-v21 , and then set it to something like this:对于第二个选项,在文件夹animator-v21 中添加一个 xml 选择器,然后将其设置为如下所示:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <objectAnimator android:duration="1"
                    android:propertyName="elevation"
                    android:valueTo="0dp"
                    android:valueType="floatType"/>
</item>

then refer it from your app bar in xml:然后在 xml 中从您的应用程序栏中引用它:

android:stateListAnimator="@animator/my_appbar_state_list_animator"

I neglected to originally provide the RecyclerView that I have under my AppBar in the question, thinking that the problem was solely the AppBarLayout.我忽略了最初提供问题中我的 AppBar 下的 RecyclerView,认为问题仅在于 AppBarLayout。 But I have since fixed the issue.但我已经解决了这个问题。 My RecyclerView looked like this:我的 RecyclerView 看起来像这样:

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="0dp"
    android:background="@drawable/gradient_blue_4"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/transactions"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:clipToPadding="false"
        android:fadeScrollbars="true"
        android:overScrollMode="always"
        android:paddingBottom="48dp"
        android:scrollbars="none"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="?selectableItemBackground" />

</android.support.v4.widget.SwipeRefreshLayout>

Underneath my AppBarLayout .在我的AppBarLayout下面。 Not exactly sure why this fixes the issue, but removing ?selectableItemBackground as the background for the RecyclerView fixes the issue and leaves me with the desired result.不完全确定为什么这解决了这个问题,但删除?selectableItemBackground作为 RecyclerView 的背景解决了这个问题,并给我留下了想要的结果。

Thanks to all provided answers, I was originally much more intrigued because I was getting my desired result by running instant run.感谢所有提供的答案,我最初更感兴趣,因为我通过运行即时运行得到了我想要的结果。 Unfortunately, this insight provided no additional help.不幸的是,这种见解没有提供额外的帮助。

set your AppBarLayout`s elevation and translationZ to zero将您的 AppBarLayout 的高度和平移Z 设置为零

appBarLayout.translationZ = 0f
appBarLayout.elevation = 0f

In your java file ..在你的java文件中..

Set A Variable for your app bar and pass in the set id using findViewById(R.id.whatever )为您的应用栏设置一个变量并使用findViewById(R.id.whatever ) 传入设置的 id

and then use然后使用

void setShadowEnabled (boolean enabled)

to disable the shadow.禁用阴影。

Hope this helps...希望这有助于...

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

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