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

前端之家收集整理的这篇文章主要介绍了如何在Android中设置按钮的样式?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在res文件夹中定义了以下文件.

styles_apptheme.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- Generated with http://android-holo-colors.com -->
  4. <resources xmlns:android="http://schemas.android.com/apk/res/android">
  5.  
  6. <style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
  7. <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
  8. </style>
  9. </resources>

themes_apptheme.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <!-- Generated with http://android-holo-colors.com -->
  4. <resources xmlns:android="http://schemas.android.com/apk/res/android">
  5.  
  6. <style name="AppTheme" parent="@style/_AppTheme"/>
  7.  
  8. <style name="_AppTheme" parent="Theme.AppCompat.Light">
  9.  
  10. <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
  11.  
  12. <item name="android:buttonStyle">@style/ButtonAppTheme</item>
  13.  
  14. </style>
  15.  
  16. </resources>

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

  1. <Button
  2. android:id="@+id/btnAddTitle"
  3. android:layout_below="@id/edEnterTitleValue"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:text="@string/btn_AddTitle"
  7. android:layout_margin="20dp"
  8. />

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

  1. android:background="@style/ButtonAppTheme"

应用程序崩溃说可以为background属性设置可绘制的资源.

所以我创建了以下drawable文件 – abc.xml

  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2. <item android:state_window_focused="false" android:state_enabled="true"
  3. android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
  4. <item android:state_window_focused="false" android:state_enabled="false"
  5. android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
  6. <item android:state_pressed="true"
  7. android:drawable="@drawable/apptheme_btn_default_pressed_holo_light" />
  8. <item android:state_focused="true" android:state_enabled="true"
  9. android:drawable="@drawable/apptheme_btn_default_focused_holo_light" />
  10. <item android:state_enabled="true"
  11. android:drawable="@drawable/apptheme_btn_default_normal_holo_light" />
  12. <item android:state_focused="true"
  13. android:drawable="@drawable/apptheme_btn_default_disabled_focused_holo_light" />
  14. <item
  15. android:drawable="@drawable/apptheme_btn_default_disabled_holo_light" />
  16. </selector>

如果我设置android:background =“@ drawable / abc”我看不到按钮中设置的样式.

所以请让我知道如何设置按钮的风格.

谢谢.

解决方法

这样做会很简单.

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

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <item android:state_pressed="true" >
  4. <shape android:shape="rectangle" >
  5. <corners android:radius="5dp" />
  6. <stroke android:width="1dip" android:color="@color/green_temp" />
  7. <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/green_temp" />
  8. </shape>
  9. </item>
  10. <item android:state_focused="true">
  11. <shape android:shape="rectangle" >
  12. <corners android:radius="5dp" />
  13. <stroke android:width="1dip" android:color="#971E05" />
  14. <solid android:color="#58857e"/>
  15. </shape>
  16. </item>
  17. <item >
  18. <shape android:shape="rectangle" >
  19. <corners android:radius="5dp" />
  20. <stroke android:width="1dip" android:color="@color/bright_green" />
  21. <gradient android:angle="-90" android:startColor="@color/green_temp" android:endColor="@color/button_green" />
  22. </shape>
  23. </item>
  24. </selector>

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

  1. <!-- Green -->
  2. <color name="green_temp">#23A96E</color>
  3. <color name="green_dark">#159204</color>
  4. <color name="bright_green">#02D8B0</color>
  5. <color name="button_green">#10a54a</color>

最后在你所需的layout.xml按钮中放置从上面的选择器的背景.

  1. <Button
  2. android:id="@+id/btnAddTitle"
  3. android:layout_below="@id/edEnterTitleValue"
  4. android:layout_width="wrap_content"
  5. android:layout_height="wrap_content"
  6. android:text="@string/btn_AddTitle"
  7. android:background="@drawable/button_selector"
  8. android:layout_margin="20dp"
  9. />

然后它的所有完成你的按钮将被设计为你想要的颜色.

猜你在找的Android相关文章