繁体   English   中英

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

[英]How to make an android raised button?

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

我希望在我的应用程序中实现以下内容,以获得更好的用户界面体验。

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

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

谢谢

在此输入图像描述

更新

            Normal button 

在此输入图像描述

and with color 

在此输入图像描述

您可以尝试以下方法:在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背景颜色。

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

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

看一下这个:-

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM