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

如何在Android中设置按钮的样式?,第1张

概述我已经在res文件夹中定义了以下文件. 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/andr 我已经在res文件夹中定义了以下文件.

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

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

在我的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"/>

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

androID:background="@style/buttonApptheme"

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

所以我创建了以下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>

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

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

谢谢.

解决方法 这样做会很简单.

首先在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>

在值文件夹的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>

最后在你所需的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"/>

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

总结

以上是内存溢出为你收集整理的如何在Android中设置按钮的样式?全部内容,希望文章能够帮你解决如何在Android中设置按钮的样式?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1131927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存