当前位置: 首页 > 知识库问答 >
问题:

Android5.0Android:高程适用于视图,但不适用按钮?

蒋典
2023-03-14

在SDK管理器的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>

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

如果我们将widget类更改为button,保留所有其他属性不变,则会丢失圆圈上的阴影:

从昨天的问题中可以看出,我们如何让android:levation按钮上获得荣誉?是否有一些android:AllowElevationToWorkasDocumented=“true”值需要放入主题或其他内容中?

共有1个答案

仰翰采
2023-03-14

Material下的默认按钮样式有一个StateListAnimator,它控制android:elevationandroid:translationz属性。您可以删除现有的动画器,也可以使用android:statelistanimator属性设置自己的动画器。

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

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

默认动画器在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>
 类似资料:
  • 问题内容: 我有一个包含两个内部div的容器div;两者在容器中的宽度和高度均应为100%。 我将两个内部div都设置为100%高度。在Firefox中效果很好,但是在IE中,div不会拉伸到100%的高度,而只会拉伸其中文本的高度。 以下是样式表的简化版本。 我做错什么了吗?还是我错过了任何Firefox / IE怪癖? 问题答案: 我认为“在Firefox中工作正常”仅在 Quirks模式下

  • 问题内容: 嗨,我只是简单地尝试在www.example.com上获取h1标签,该标签显示为“ Example Domain”。该代码适用于http://www.example.com,但不适用于https://www.exmaple.com。我该如何解决这个问题?谢谢 问题答案: PhantomJSDriver不支持(所有)DesiredCapabilities。 你会需要: 记录在这里:htt

  • 所以我使用这种方法写入文件,它在windows上运行完全正常,但在mac上运行时,它会创建文件,但它们是空的。 我知道数据是正确的,因为它打印正确。感谢您的任何帮助,这真的让我绊倒了。

  • 列名称的类型为int[] 上述查询适用于postgresql,但不适用于hsqldb,甚至适用于sql 尝试的hsqldb版本:2.2.9和2.3.0 在hsqldb中工作的sql是从table_name中选择x,unnest(column_name)y(x)x和y不是该表的列。

  • 当我在Android手机上调试应用程序时,我绝对没有遇到任何错误,但在AVD上,我一开始就得到了一个NullPointerException()。我的Android设备在22 API的Lollipop(5.1.1)上运行,我的AVD在28 API的Pie(9.0)上运行。我在gradle中将最小SDK设置为21,编译SDK设置为28。 这是错误: E/AndroidRuntime:致命异常:主进程

  • 我有一个C程序,通过响应信号来运行。一些信号导致父级分叉。这允许在父级继续响应信号的同时进行其他处理。 当父母被发送SIGTERM时,我希望分叉的孩子也收到一个SIGTERM。在父级退出之前,子级完成处理 SIGTERM 并不重要。 但是,使用下面的代码,当我从父代调用< code>kill(0,SIGTERM)时,子代没有收到SIGTERM。从< code>kill联机帮助页来看,似乎所有的孩子