简体   繁体   English

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

[英]How to set Style for a button in Android?

I have defined the below files in res folder. 我在res文件夹中定义了以下文件。

styles_apptheme.xml styles_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. 应用程序崩溃说应该为background属性设置drawable资源。

So i created the below drawable file - abc.xml 所以我创建了下面的drawable文件 - 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. 谢谢。

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. 然后完成所有按钮将使用您想要的颜色设置样式。

Android Button Maker is online tool to generate buttons code for Android Apps. Android Button Maker是在线工具,可为Android Apps生成按钮代码。 Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients. Android API提供可绘制资源,其中XML文件定义几何形状,包括颜色,边框和渐变。 These button is generating based on shape drawable XML code which load faster compare to normal png buttons. 这些按钮基于形状可绘制的XML代码生成,与普通的png按钮相比加载速度更快。 You can customize button properties in setting panel and get source code. 您可以在设置面板中自定义按钮属性并获取源代码。

Check this link Button Maker 检查此链接Button Maker

No need to crack brains...this tool make it simple 无需破解大脑...这个工具使它变得简单

replace 更换

android:background="@style/ButtonAppTheme"

with

style="@style/ButtonAppTheme"

and every thing is ok! 一切都好!

Try this out as alternative. 尝试这个替代方案。 create a drawable folder under res then create xml files and copypaste below code and try first then you customize as per your requirement. res下创建一个drawable文件夹,然后在代码下创建xml文件和copypaste,然后先尝试根据您的要求进行自定义。

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. 您可以通过这种方式自定义整个按钮样式。

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>

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

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