海拔不起作用

时间:2014-12-19 18:49:25

标签: android material-design

我正在尝试在示例应用中的按钮上获得4dp提升,但我似乎无法显示任何阴影。

这是我的代码:

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

我的背景是:

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

我的app.gradle是:

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

结果如下:

No drop shadow

我确实看到其他帖子的问题与此问题相同:Elevation on Android Lollipop not working。它们都指向半透明背景作为问题,但正如你在这里看到的那样,我使用的是完全不透明的背景。

知道我缺少什么吗?

1 个答案:

答案 0 :(得分:0)

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:

enter image description here