安卓5.0安卓:海拔Works的视图,而不是按钮? [英] Android 5.0 android:elevation Works for View, but not Button?

查看:322
本文介绍了安卓5.0安卓:海拔Works的视图,而不是按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从SDK管理器的Andr​​oid 5.0的样品,还有就是 ElevationBasic 样本。它显示了两个查看的对象:一个圆形和正方形。圆了安卓海拔设置为 30dp

 < XML版本=1.0编码=UTF-8&GT?;
<! - 
 版权所有2014年的Andr​​oid开源项目

 Apache许可证下授权,版本2.0(以下简称许可证);
 你可能不使用这个文件除了在遵守许可。
 您可以在获得许可证的副本

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

 除非适用法律要求或书面同意,软件
 该许可证下发布分布在原样的基础上,
 无担保或任何形式的条件,无论是EX preSS或暗示的保证。
 查看许可证的特定语言的管理权限和
 该许可证下的限制。
 - >

<的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
             的xmlns:工具=htt​​p://schemas.android.com/tool​​s
             机器人:layout_width =match_parent
             机器人:layout_height =match_parent>
    <查看
            机器人:ID =@ + ID / floating_shape
            机器人:layout_width =80dp
            机器人:layout_height =80dp
            机器人:layout_marginRight =40dp
            机器人:背景=@可绘制/形状
            机器人:海拔=30dp
            机器人:layout_gravity =中心/>
    <查看
            机器人:ID =@ + ID / floating_shape_2
            机器人:layout_width =80dp
            机器人:layout_height =80dp
            机器人:layout_marginLeft =25dp
            机器人:背景=@可绘制/ shape2
            机器人:layout_gravity =中心/>
< /的FrameLayout>
 

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

如果我们的Widget类改为按钮,使所有其他属性-是,我们失去了对圆的阴影:

的问题:

  1. 为什么安卓海拔行为的改变?它不能是由于背景,因为它是在相同的背景中这两种情况下

  2. 哪些类支持安卓海拔,哪些没有?例如,使用的TextView 而不是查看按钮仍给我们的阴影,所以这种行为上的变化不会起用了的TextView 的水平,而在按钮水平。

  3. 正如在这个问题,我们怎么得到安卓海拔将在按钮荣幸?有一些安卓?allowElevationToWorkAsDocumented =真正的的价值,我们必须把在一个主题或某事

解决方案

材质下的默认按钮样式有一个<一个href="https://developer.android.com/reference/android/animation/StateListAnimator.html">StateListAnimator控制在安卓海拔安卓translationZ 属性。您可以删除现有动画或使用 Android的设置自己:stateListAnimator 属性

 &LT;按钮
    ...
    机器人:stateListAnimator =@空/&GT;

&LT;按钮
    ...
    机器人:stateListAnimator =@动画/ my_animator/&GT;
 

默认的动画在<定义href="https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/anim/button_state_list_anim_material.xml">button_state_list_anim_material.xml.这里是在启用和pressed状态的例子:

 &LT; XML版本=1.0编码=UTF-8&GT?;
&LT;选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
    &LT;项目的android:STATE_ pressed =真正的机器人:state_enabled =真正的&GT;
        &LT;集&gt;
            &LT; objectAnimator机器人:propertyName的=translationZ
                            机器人:时间=@整数/ button_ pressed_animation_duration
                            机器人:valueTo =@扪/ button_ pressed_z_material
                            机器人:值类型=floatType/&GT;
            &LT; objectAnimator机器人:propertyName的=海拔
                            机器人:时间=0
                            机器人:valueTo =@扪/ button_elevation_material
                            机器人:值类型=floatType/&GT;
        &LT; /集&gt;
    &LT; /项目&GT;
    &LT;! - 基态 - &GT;
    &LT;项目安卓state_enabled =真正的&GT;
        &LT;集&gt;
            &LT; objectAnimator机器人:propertyName的=translationZ
                            机器人:时间=@整数/ button_ pressed_animation_duration
                            机器人:valueTo =0
                            机器人:startDelay所占用=@整数/ button_ pressed_animation_delay
                            机器人:值类型=floatType/&GT;
            &LT; objectAnimator机器人:propertyName的=海拔
                            机器人:时间=0
                            机器人:valueTo =@扪/ button_elevation_material
                            机器人:值类型=floatType/&GT;
        &LT; /集&gt;
    &LT; /项目&GT;
    ...
&LT; /选择器&GT;
 

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>

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

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

The questions:

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

  2. 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.

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

解决方案

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" />

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>

这篇关于安卓5.0安卓:海拔Works的视图,而不是按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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