Android 5.0 android:提升适用于View,但不适用于Button?

Com*_*are 65 android android-5.0-lollipop

在SDK Manager的Android 5.0示例中,有ElevationBasic示例.它显示了两个View对象:圆形和方形.圆圈android:elevation设置为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>
Run Code Online (Sandbox Code Playgroud)

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

ElevationBasic,原文为

如果我们将窗口小部件类更改为Button,将所有其他属性保留为原样,则会丢失圆圈上的阴影:

ElevationBasic,使用按钮

问题:

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

  2. 哪些类支持android:elevation,哪些不支持?例如,使用TextView代替ViewButton仍然给我们投影,所以行为的这种改变不会在TextView级别引入,而是在Button级别引入.

  3. 从昨天的这个问题可以看出,我们如何才能获得android:elevation荣誉Buttonandroid:allowElevationToWorkAsDocumented="true"我们必须在主题或其他东西中放置一些价值吗?

ala*_*anv 126

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

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

<Button
    ...
    android:stateListAnimator="@anim/my_animator" />
Run Code Online (Sandbox Code Playgroud)

默认动画师在button_state_list_anim_material.xml中定义.以下是显示启用和按下状态的示例:

<?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>
Run Code Online (Sandbox Code Playgroud)


GPa*_*ack 13

根据我在Lollipop设备上运行的Appcompat v7的使用经验,Button可以使用默认功能,如点击时的涟漪效果,高程和z动画,但如果android:background在xml元素中设置了个性化属性(作为颜色或选择器),则会错过它们.


Hei*_*erg 7

这是因为您手动设置按钮的背景,这将取代其所有效果.

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

    <Button
  ...
  style="@style/Widget.AppCompat.Button.Colored" />
Run Code Online (Sandbox Code Playgroud)

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


小智 5

我有一个类似的问题,我认为是由于错误夸大的布局,但似乎添加clipToPadding做了伎俩.必须将其设置为ViewGroup包含要投射阴影的视图的父级.

... android:clipToPadding="false" ...