Android 5.0 android:elevation 适用于视图,但不适用于 Button? [英] Android 5.0 android:elevation Works for View, but not Button?

查看:41
本文介绍了Android 5.0 android:elevation 适用于视图,但不适用于 Button?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在来自 SDK 管理器的 Android 5.0 示例中,有 ElevationBasic 示例.它显示了两个 View 对象:一个圆形和一个正方形.圆圈将 android:elevation 设置为 30dp:

In the Android 5.0 samples from the SDK Manager, there is the ElevationBasic sample. It shows two View objects: a circle and a square. The circle has android:elevation set to 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 上,按原样运行示例,我们会在圆圈上看到一个阴影:

On a Nexus 9, running the sample as-is, we get a drop shadow on the circle:

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

If we change the widget class to Button, leaving all other attributes as-is, we lose the drop shadow on the circle:

问题:

  1. 为什么 android:elevation 行为会发生变化?不可能是背景造成的,因为在这两种情况下都是相同的背景.

  1. Why is the android:elevation behavior changing? It cannot be due to the background, because it is the same background in both cases.

哪些类支持 android:elevation,哪些不支持?例如,使用 TextView 而不是 ViewButton 仍然会给我们带来阴影,所以这种行为变化不会在 >TextView 级别,而是在 Button 级别.

Which classes support android:elevation, and which do not? For example, using TextView instead of View or Button still gives us the drop shadow, so this change in behavior is not introduced at the TextView level, but rather at the Button level.

正如昨天的这个问题中所见,我们如何得到android:elevationButton 上获得荣誉?是否有一些 android:allowElevationToWorkAsDocumented="true" 值我们必须放入主题或其他内容中?

As seen in this question from yesterday, how do we get android:elevation to be honored on a Button? Is there some android:allowElevationToWorkAsDocumented="true" value that we have to put in a theme or something?

推荐答案

Material 下默认的 Button 样式有一个 StateListAnimator 控制 android:elevationandroid:translationZ 属性.您可以使用 android:stateListAnimator 属性删除现有的动画师或设置自己的动画师.

The default Button style under Material has a StateListAnimator that controls the android:elevation and android:translationZ properties. You can remove the existing animator or set your own using the android:stateListAnimator property.

<Button
    ...
    android:stateListAnimator="@null" />

<Button
    ...
    android:stateListAnimator="@anim/my_animator" />

默认动画器定义在 button_state_list_anim_material.xml.这是一个显示启用和按下状态的示例:

The default animator is defined in button_state_list_anim_material.xml. Here is a sample showing the enabled and pressed states:

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

这篇关于Android 5.0 android:elevation 适用于视图,但不适用于 Button?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆