如何为Android中的按钮设置样式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26070827/
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-20 09:58:18  来源:igfitidea点击:

How to set Style for a button in Android?

androidandroid-layoutandroid-buttonandroid-styles

提问by Prem

I have defined the below files in res folder.

我在 res 文件夹中定义了以下文件。

styles_apptheme.xml

style_apptheme.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
      <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
  </style> 
</resources>

themes_apptheme.xml

themes_apptheme.xml

<?xml version="1.0" encoding="utf-8"?>

<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

  <style name="AppTheme" parent="@style/_AppTheme"/>

  <style name="_AppTheme" parent="Theme.AppCompat.Light">

    <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>

    <item name="android:buttonStyle">@style/ButtonAppTheme</item>

  </style>

</resources>

In my Layout.xml file, i have defined my button as below

在我的 Layout.xml 文件中,我定义了我的按钮如下

<Button
        android:id="@+id/btnAddTitle"
        android:layout_below="@id/edEnterTitleValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btn_AddTitle"
        android:layout_margin="20dp"
/>

If i add the below line to the button view above,

如果我将下面的行添加到上面的按钮视图中,

android:background="@style/ButtonAppTheme"

Application crashes saying that drawable resource should be set for the background attribute.

应用程序崩溃说应为背景属性设置可绘制资源。

So i created the below drawable file - abc.xml

所以我创建了以下可绘制文件 - abc.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_window_focused="false" android:state_enabled="true"
        android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
    <item android:state_window_focused="false" android:state_enabled="false"
        android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
    <item android:state_pressed="true"
        android:drawable="@drawable/apptheme_btn_default_pressed_holo_light" />
    <item android:state_focused="true" android:state_enabled="true"
        android:drawable="@drawable/apptheme_btn_default_focused_holo_light" />
    <item android:state_enabled="true"
        android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
    <item android:state_focused="true"
        android:drawable="@drawable/apptheme_btn_default_disabled_focused_holo_light" />
    <item
         android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
</selector>

If i set android:background="@drawable/abc"i dont see the style set in the button.

如果我设置, android:background="@drawable/abc"我看不到按钮中设置的样式。

So please let me know how i can set the style for the button.

所以请让我知道如何设置按钮的样式。

Thanks.

谢谢。

回答by Crawler

It would be rather simple if you do it this way.

如果你这样做,那将是相当简单的。

First create a button_selector.xml in drawable folder.

首先在 drawable 文件夹中创建一个 button_selector.xml。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" >
 <shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="@color/green_temp" />
     <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp"  />            
 </shape>
</item>
<item android:state_focused="true">
 <shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="#971E05" />
     <solid android:color="#58857e"/>       
 </shape>
</item>  
<item >
<shape android:shape="rectangle"  >
     <corners android:radius="5dp" />
     <stroke android:width="1dip" android:color="@color/bright_green" />
     <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />            
 </shape>
</item>
</selector>

Add these colors in colors.xml of values folder.

在 values 文件夹的 colors.xml 中添加这些颜色。

<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>

Finally in your desired button of layout.xml put background from above selector.

最后在你想要的 layout.xml 按钮中放置选择器上方的背景。

<Button
    android:id="@+id/btnAddTitle"
    android:layout_below="@id/edEnterTitleValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_AddTitle"
    android:background="@drawable/button_selector"
    android:layout_margin="20dp"
/>

Then its all done your button will be styled with color you want.

然后一切都完成了,您的按钮将使用您想要的颜色进行设计。

回答by Harsha Vardhan

Android Button Maker is online tool to generate buttons code for Android Apps. Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients. These button is generating based on shape drawable XML code which load faster compare to normal png buttons. You can customize button properties in setting panel and get source code.

Android Button Maker 是为 Android 应用程序生成按钮代码的在线工具。Android API 提供可绘制资源,其中 XML 文件定义几何形状,包括颜色、边框和渐变。这些按钮是基于形状可绘制的 XML 代码生成的,与普通的 png 按钮相比,加载速度更快。您可以在设置面板中自定义按钮属性并获取源代码。

Check this link Button Maker

检查此链接按钮制造商

No need to crack brains...this tool make it simple

无需破解大脑......这个工具使它变得简单

回答by behnamizadi

replace

代替

android:background="@style/ButtonAppTheme"

with

style="@style/ButtonAppTheme"

and every thing is ok!

一切正常!

回答by Kavin Prabhu

Try this out as alternative. create a drawablefolder under resthen create xml files and copypaste below code and try first then you customize as per your requirement.

试试这个作为替代。在res下创建一个drawable文件夹,然后创建 xml 文件并复制下面的代码,然后先尝试,然后根据您的要求进行自定义。

button_bg.xml

button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">

<solid android:color="#00acc1"/>
<stroke android:width="2dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>

button_bg_pressed.xml

button_bg_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">

<solid android:color="#006064"/>
<stroke android:width="1dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>

button_selector.xml

button_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:state_focused="true" android:state_pressed="true" 
        android:drawable="@drawable/button_bg_pressed" /> 
<item android:state_focused="false" android:state_pressed="true" 
        android:drawable="@drawable/button_bg_pressed" /> 
<item android:drawable="@drawable/button_bg" /> </selector>

And now in your button xml set the background as button_selector.xml

现在在您的按钮 xml 中将背景设置为 button_selector.xml

<Button
    android:id="@+id/btnAddTitle"
    android:layout_below="@id/edEnterTitleValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_AddTitle"
    android:background="@drawable/button_selector"
    android:layout_margin="20dp"/>

This will do the job for you. You can customize your entire button style by this way.

这将为您完成这项工作。您可以通过这种方式自定义整个按钮样式。

回答by Ting

If you want to have the default animation of button by material design and change button color, try this.

如果您想通过材料设计获得按钮的默认动画并更改按钮颜色,请尝试此操作。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_12"
    android:text="button text here"
    android:theme="@style/NavyBtn" />

in style.xml

在 style.xml 中

<style name="NavyBtn" parent="Theme.AppCompat.Light">
    <item name="colorButtonNormal">#020e39</item>
    <item name="android:textColor">#FFFFFF</item>
</style>