如何制作一个Android凸起按钮?

时间:2015-03-28 08:13:56

标签: android android-5.0-lollipop android-button

我已经在网上搜索了一个星期左右,以获得一个关于如何制作彩色凸起按钮但没有运气的示例或教程。

我希望在我的应用中实现以下功能,以获得更好的用户界面体验。

我确实遇到过卡片视图但是当你编写一个包含大量按钮的大程序时,xml代码会因为这个卡片视图而变得更大。

因此,如果有以下任何快速简单的解决方案,请告诉我。

谢谢

enter image description here

更新

            Normal button 

enter image description here

and with color 

enter image description here

4 个答案:

答案 0 :(得分:5)

你可以试试这个替代方案: 在 drawable 文件夹中创建xml: cardlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape android:shape="rectangle">
            <gradient android:angle="90"
                android:endColor="@color/background_gray" android:startColor="#ccc" />
            <corners android:radius="4dp" />
        </shape>
    </item>

    <item
        android:left="0dp"
        android:right="1.5dp"
        android:top="0dp"
        android:bottom="1.5dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white"/>
            <corners android:radius="4dp" />
        </shape>
    </item>
</layer-list>

现在要提升的任何view(Button,ListRow或imageView)。只需使用此cardlayout设置该视图的背景。

android:background="@drawable/cardlayout"

NB根据您的需要更改cardlayouts背景颜色。

答案 1 :(得分:5)

我认为你实际上想要提升按钮。不要使用卡,因为它需要更多资源。对于棒棒糖设备使用

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

并在资源中的anim文件夹中创建my_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>
    ...
</selector>

答案 2 :(得分:3)

检查出来: -

<Button
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="60dp"
    android:text="Raised Color Button"
    android:textSize="15sp"
    android:backgroundTint="#1E9D96"
    android:textColor="@android:color/background_light" />

答案 3 :(得分:0)

您可以使用:     android:elevation="2dp"

但它只适用于棒棒糖及以上。我建议在较低版本的Android版本中使用图层列表。