Android 5.0的android:海拔为视图,但不是button?

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

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

ElevationBasic,原文为

如果我们将widget类更改为Button ,将所有其他属性保持原样,则我们会在圆上丢失阴影:

ElevationBasic,使用按钮

问题:

  1. 为什么android:elevation行为改变? 这不能归因于背景,因为在两种情况下都是相同的背景。

  2. 哪些类支持android:elevation ,哪些不提供? 例如,使用TextView而不是ViewButton仍然会给我们留下阴影,所以这种行为变化不是在TextView级别引入的,而是在Button级别引入的。

  3. 从昨天的这个问题可以看出,我们如何获得android:elevation在一个Button上得到尊重? 是否有一些android:allowElevationToWorkAsDocumented="true"值,我们必须把一个主题或东西?

Material下的默认button样式有一个StateListAnimator ,用于控制android:elevationandroid:translationZ属性。 您可以使用android:stateListAnimator属性来移除现有的animation设置或设置您自己的设置。

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

默认的animation在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> 

在我使用棒棒糖设备运行的Appcompat v7的经验中, Button默认function如点击时的纹波效果,高程和zanimation,但如果在xml元素中设置了个性化的android:background属性(如颜色或select器),则会错过。

这是因为你手动设置button的背景,将取代所有的效果。

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

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

如果你想要指定不同的颜色,你可以创build一个新的主题,并通过android:theme将其应用到button。 那么你可以在你想要相同效果的所有button上使用这个主题。

我有一个类似的问题,我认为是由于错误的膨胀布局,但似乎添加clipToPadding做的伎俩。 这必须设置为包含要投射阴影的视图的父视图组。

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