Java Android studio - 为按钮添加样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22600528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:45:37  来源:igfitidea点击:

Android studio- Adding style to buttons

javaandroidxmlbuttonstyles

提问by lehermj

I want to add a shadow and have rounded edges on my button, like box-shadow and border radius in CSS how can I do this in XML? XML code:

我想在我的按钮上添加阴影和圆角边缘,比如 CSS 中的框阴影和边框半径,我如何在 XML 中做到这一点?XML 代码:

    <Button android:id="@+id/dummy_button"
    style="?metaButtonBarButtonStyle"
    android:layout_width="198dp"
    android:layout_height="69dp"
    android:layout_weight="1"
    android:text="@string/dummy_button"
    android:fontFamily="trebuchet ms"
    android:clickable="true"
    android:onClick="printStarter"
    android:background="#8c96ff"
    android:layout_gravity="center_horizontal|bottom" />

采纳答案by Lokesh

The Android Developer's Guide has a detailed guide on this: Shape Drawbables.

Android 开发人员指南对此有详细指南:Shape Drawbables。

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="3dp" />
    <stroke android:width="5px" android:color="#1a1a1a" />

button with shadow

带阴影的按钮

<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">

   <item android:drawable="@drawable/shadow"/>
   <item
     android:drawable="@drawable/button"
     android:bottom="4px" /> 

</layer-list>
    </shape>

Refer this tutorial

参考本教程