android – 在整个应用程序中更改按钮样式

我正在尝试将我的应用程序中所有按钮的TextColor更改为白色,并尝试将其设置为粗体.但这没有发生,我正在覆盖 android:Widget.Button
我正在为Jelly Bean 4.1.2开发
我究竟做错了什么?

清单中的主题定义

android:theme="@style/spui" >

我喜欢的主题

<style name="spui" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:buttonStyle">@style/Buttonspui</item>
</style>

按钮本身的样式

<style name="Buttonspui" parent="android:Widget.Button">
      <item name="android:background">@drawable/btn_default_holo_light</item>
      <item name="android:minHeight">48dip</item>
      <item name="android:minWidth">64dip</item>
      <item name="android:textColor">#ffffff</item>
      <item name="android:textStyle">bold</item>
  </style>

按钮

<Button
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginTop="15dp"
     android:text="@string/edit"
     android:id="@+id/btnEdit"
     style="@style/Buttonspui"/>

解决方法

要设置Button的样式,您可以使用:

转到drawable文件夹并创建一个名为“style”的XML(例如button.xml)文件,其中包含以下内容:

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">  

    <item>
         <shape>
             <gradient android:startColor="#449def" android:endColor="#2f6699" android:angle="270" />
             <stroke   android:width="1px"          android:color="#000000" />  
                <corners android:bottomLeftRadius="0dp"
                android:bottomRightRadius="0dp"
                android:topLeftRadius="8dp"
                android:topRightRadius="8dp"/>       
              <padding  android:left="10dp"  android:top="10dp" android:right="10dp" android:bottom="10dp" />
         </shape>  
   </item> 

</selector>

这是我的代码,您可以进行所需的必要更改

现在在你的布局XML(mainactivity.xml)中调用它

android:background="@drawable/button.xml"

现在要更改字体颜色和样式,您可以使用以下值作为values文件夹中的styles.xml的一部分

<style name="buttonStyle" parent="@android:style/Widget.Button.Small">
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:textSize">12sp</item>
    <item name="android:textStyle">bold</item>
</style>

现在在布局XML(mainactivity.xml)中调用它,就像这样

style="@style/buttonStyle"

最终的代码是:

<Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="15dp"
 android:text="@string/edit"
 android:id="@+id/btnEdit"
 android:background="@drawable/button.xml"
 style="@style/buttonStyle"
/>

希望这可以帮助 :)

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...