Android L Raised Normal Button

﹥>﹥吖頭↗ 提交于 2019-12-24 11:39:22

问题


I've been hunting high and low for a way to get a "raised" rounded rectangular button without completely doing it myself using backgrounds and a custom view. I want buttons like this:

I thought this would do it:

<RelativeLayout 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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter the Danger Zone"
        android:id="@+id/btn"
        android:elevation="8dp" <!-- Here -->
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"/>

</RelativeLayout>

But this does nothing. The button looks the same. WTF?

And yes, I'm using the latest SDK, etc. etc.

Edit: So I added a translationZ. Now, a shadow appears as the activity is opening, but disappears when the animation is done. ???


回答1:


Try adding state list animator:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:state_enabled="true"
    android:state_pressed="true">
    <set>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="8dp"
            android:valueType="floatType" />
    </set>
</item>
<item>
    <set>
        <objectAnimator
            android:duration="@android:integer/config_shortAnimTime"
            android:propertyName="translationZ"
            android:valueTo="2dp"
            android:valueType="floatType" />
    </set>
</item>
</selector>



回答2:


I had a similar problem i thought was due to wrongly inflated layouts, but it appeared that adding clipToPadding did the trick. This must be set to the parent ViewGroup containing the view you want to cast a shadow.

<YourParentLayout
...
android:clipToPadding="false"
...

also if your view has no background, use android:outlineProvider="bounds"



来源:https://stackoverflow.com/questions/27070436/android-l-raised-normal-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!