海拔不工作海拔、工作

2023-09-06 07:33:40 作者:■D范二少年べ孓

我试图让一个按钮 4DP 抬高一个示例应用程序,我似乎无法得到任何的影子出现。

这是我的code:

 < LinearLayout中的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          工具:上下文=。MainActivity          机器人:clipChildren =假          机器人:方向=垂直>    <按钮        机器人:layout_width =WRAP_CONTENT        机器人:layout_height =WRAP_CONTENT        机器人:文字=点击我        机器人:背景=@绘制/ myrect        机器人:海拔=4DP/>< / LinearLayout中> 

使用我的背景绘制的存在:

 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android   机器人:形状=矩形>    [固体机器人:颜色=#336699/>    <角落的an​​droid:半径=5DP/>< /形状> 
返藏工作,这份高海拔防疫攻略要收好

我的app.gradle是:

 安卓{    compileSdkVersion 21    buildToolsVersion21.1.1    ...} 

这是结果:

我没有看到其他职位,像这样一个同样的问题:Elevation Android上的棒棒糖不工作。他们都指向一个半透明背景的问题,但你可以在这里看到我使用的是完全不透明的。

我失去了我的任何想法?

解决方案

有关Android版本5.0和放大器;上述尝试高程其他视图。

 的android:海拔=10dp 

有关按钮,

 的android:stateListAnimator =@动画/ button_state_list_animator 

button_state_list_animator.xml

\r\r

<?XML版本=1.0编码=UTF-8? >\r<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>\r<项目的android:STATE_ pressed =真正的机器人:state_enabled =真正的>\r<集>\r< objectAnimator机器人:propertyName的=translationZ\r机器人:时间=@整数/ button_ pressed_animation_duration\r机器人:valueTo =@扪/ button_ pressed_z_material\r机器人:VALUETYPE =floatType/>\r< objectAnimator机器人:propertyName的=海拔\r机器人:时间=0\r机器人:valueTo =@扪/ button_elevation_material\r机器人:VALUETYPE =floatType/>\r< /集>\r< /项目>\r<! - 基态 - >\r<项目的android:state_enabled =真正的>\r<集>\r< objectAnimator机器人:propertyName的=translationZ\r机器人:时间=@整数/ button_ pressed_animation_duration\r机器人:valueTo =0\r机器人:startDelay所=@整数/ button_ pressed_animation_delay\r机器人:VALUETYPE =floatType/>\r< objectAnimator机器人:propertyName的=海拔\r机器人:时间=0\r机器人:valueTo =@扪/ button_elevation_material\r机器人:VALUETYPE =floatType/>\r< /集>\r< /项目>\r<项目>\r<集>\r< objectAnimator机器人:propertyName的=translationZ\r机器人:时间=0\r机器人:valueTo =0\r机器人:VALUETYPE =floatType/>\r< objectAnimator机器人:propertyName的=海拔\r机器人:时间=0\r机器人:valueTo =0\r机器人:VALUETYPE =floatType/>\r< /集>\r< /项目>\r< /选择>

\r\r\r

低于5.0的版本,

 的android:背景=@机器人:可绘制/ dialog_holo_light_frame 

我的输出:

I'm trying to get a 4dp elevation on a button in a sample app and I can't seem to get any shadow to show up.

This is my code:

<LinearLayout 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"
          tools:context=".MainActivity"
          android:clipChildren="false"
          android:orientation="vertical">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click me"
        android:background="@drawable/myrect"
        android:elevation="4dp"/>
</LinearLayout>

With my background drawable being:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
    <solid android:color="#336699" />
    <corners android:radius="5dp" />
</shape>

My app.gradle is:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"
    ...
}

This is the result:

I did see other posts with the same issue like this one: Elevation on Android Lollipop not working. They all point to a semi-transparent background as the issue, but as you can see here I'm using a full opacity one.

Any idea of what I'm missing?

解决方案

For android version 5.0 & above try the Elevation for other views..

android:elevation="10dp"

For Buttons,

android:stateListAnimator="@anim/button_state_list_animator"

button_state_list_animator.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>
<item>
<set>
<objectAnimator android:propertyName="translationZ"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
<objectAnimator android:propertyName="elevation"
android:duration="0"
android:valueTo="0"
android:valueType="floatType"/>
</set>
</item>
</selector>

below 5.0 version,

 android:background="@android:drawable/dialog_holo_light_frame"

My output: