Android 安卓5.0安卓:立面用于视图,但不用于按钮?

Android 安卓5.0安卓:立面用于视图,但不用于按钮?,android,android-5.0-lollipop,Android,Android 5.0 Lollipop,在SDK管理器的Android 5.0示例中,有ElevationBasic示例。它显示两个视图对象:一个圆和一个正方形。圆圈将安卓:高程设置为30dp: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright 2014 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you

在SDK管理器的Android 5.0示例中,有
ElevationBasic
示例。它显示两个
视图
对象:一个圆和一个正方形。圆圈将安卓:高程设置为
30dp

<?xml version="1.0" encoding="utf-8"?>
<!--
 Copyright 2014 The Android Open Source Project

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:tools="http://schemas.android.com/tools"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <View
            android:id="@+id/floating_shape"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginRight="40dp"
            android:background="@drawable/shape"
            android:elevation="30dp"
            android:layout_gravity="center"/>
    <View
            android:id="@+id/floating_shape_2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_marginLeft="25dp"
            android:background="@drawable/shape2"
            android:layout_gravity="center"/>
</FrameLayout>

在Nexus 9上,按原样运行示例,我们在圆上得到一个阴影:

如果我们将小部件类更改为
按钮
,保留所有其他属性不变,我们将丢失圆圈上的阴影:

问题是:

  • 为什么android:elevation的行为会发生变化?这不能归因于背景,因为在这两种情况下背景是相同的

  • 哪些类支持android:elevation,哪些不支持?例如,使用
    TextView
    而不是
    View
    按钮
    仍然会给我们带来阴影,因此这种行为变化不是在
    TextView
    级别引入的,而是在
    按钮
    级别引入的

  • 正如从昨天开始看到的,我们如何通过
    按钮
    ,获得android:elevation的荣誉?是否有一些
    android:allowElevationToWorkAsDocumented=“true”
    值必须放在主题或其他内容中


  • “材质”下的默认按钮样式具有控制
    android:elevation
    android:translationZ
    属性的按钮样式。您可以删除现有的动画师或使用
    android:stateListAnimator
    属性设置自己的动画师

    <Button
        ...
        android:stateListAnimator="@null" />
    
    <Button
        ...
        android:stateListAnimator="@anim/my_animator" />
    
    
    
    在中定义了默认动画制作工具。以下是显示启用和按下状态的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:state_enabled="true">
            <set>
                <objectAnimator android:propertyName="translationZ"
                                android:duration="@integer/button_pressed_animation_duration"
                                android:valueTo="@dimen/button_pressed_z_material"
                                android:valueType="floatType"/>
                <objectAnimator android:propertyName="elevation"
                                android:duration="0"
                                android:valueTo="@dimen/button_elevation_material"
                                android:valueType="floatType"/>
            </set>
        </item>
        <!-- base state -->
        <item android:state_enabled="true">
            <set>
                <objectAnimator android:propertyName="translationZ"
                                android:duration="@integer/button_pressed_animation_duration"
                                android:valueTo="0"
                                android:startDelay="@integer/button_pressed_animation_delay"
                                android:valueType="floatType"/>
                <objectAnimator android:propertyName="elevation"
                                android:duration="0"
                                android:valueTo="@dimen/button_elevation_material"
                                android:valueType="floatType" />
            </set>
        </item>
        ...
    </selector>
    
    
    ...
    
    根据我在棒棒糖设备上运行Appcompat v7的经验,
    按钮
    使用默认功能,如点击时的涟漪效果、提升和z动画,但如果设置了个性化的
    android:background
    属性(如颜色或选择器),则会忽略这些功能在xml元素中。

    我有一个类似的问题,我认为是由于错误地夸大了布局,但似乎添加了
    cliptopladding
    。 这必须设置为包含要投射阴影的视图的父视图组

    。。。
    android:clipToPadding=“false”
    
    这是因为您正在手动设置按钮的背景,它将替换按钮的所有效果

    从版本23.0.0发布的AppCompat开始,有一个新的小部件.AppCompat.Button.Colored样式,它使用主题的colorButtonNormal表示禁用的颜色,colorAccent表示启用的颜色

        <Button
      ...
      style="@style/Widget.AppCompat.Button.Colored" />
    
    
    

    如果您想要不同于指定颜色的颜色,可以创建一个新主题,并通过
    android:theme
    将其应用于按钮。然后,您可以在所有需要相同效果的按钮上使用此主题。

    此解决方案适用于所有Android API版本

    创建阴影
    @android:drawable/dialog\u holo\u light\u frame
    &如果要自定义背景颜色而不是白色,请在阴影顶部创建具有可自定义颜色的图层列表背景,如下所示

    创建一个单独的可绘制文件white\u background\u shadow.xml

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!--the shadow comes from here-->
        <item
            android:bottom="0dp"
            android:drawable="@android:drawable/dialog_holo_light_frame"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp">
    
        </item>
    
        <item
            android:bottom="0dp"
            android:left="0dp"
            android:right="0dp"
            android:top="0dp">
            <!--whatever you want in the background, here i preferred solid white -->
            <shape android:shape="rectangle">
                <solid android:color="@android:color/white" />
    
            </shape>
        </item>
    </layer-list>
    

    有趣的问题和发现,您能与我们分享测试项目的任何变化吗?@Rolfツ: 同样,您可以从SDK管理器下载Android 5.0示例中的
    ElevationBasic
    示例。我没有编写示例,尽管我按照
    按钮测试的问题中的说明对其进行了修改。我的错误是,我读得有点快;)这个问题是关于本机Android 5.0实现的,而不是appcompat-v7。不过,谢谢!也许是一样的,appcompat v7通过运行在Android 5上的平台主题继承了Material的样式。同样,如果有人知道答案,这将非常有用!谢谢这使得平面按钮在pre-L设备上看起来可能是一样的。@Commonware hi我使用了它,但它没有显示任何帮助?伙计,你救了我的命,使用android:stateListAnimator=“@null”我修复了z顺序的所有问题,animator重写这些值在很多级别上都是错误的…:(开发者完全被蒙在鼓里,不知道为什么按钮会有不同的仰角或z平移。这个问题是关于本机Android 5.0的实现,而不是
    appcompat-v7
    。不过,谢谢!@Heisenberg我尝试过添加这个按钮,但没有在图像按钮上添加阴影。有什么提示吗?我尝试过使用本机ImageButton和AppCompatImageButton。在6.0上测试。
    android:background="@drawable/shadow"