【问题标题】:Changing button style in the whole application在整个应用程序中更改按钮样式
【发布时间】:2013-06-19 13:10:32
【问题描述】:

我正在尝试将我的应用程序中按钮的所有 TextColor 更改为白色,并尝试使其变为粗体。但这没有发生,我正在覆盖the 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"/>

【问题讨论】:

  • 尝试将该样式应用于每个按钮。这将起作用。
  • 如果您在应用程序中应用此主题,请确保您没有为活动或按钮本身应用另一个主题。
  • 按钮本身没有其他样式
  • 通常我不应该为按钮定义样式,因为我覆盖了我定义为样式父级的android:Widget.Button

标签: android android-manifest android-button android-styles


【解决方案1】:

要为您的 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"

现在要更改字体颜色和样式,您可以使用值文件夹中 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"
/>

希望这会有所帮助:)

【讨论】:

  • 这是正确的答案。但仍然无法相信 Google 没有提供更一致的方式来为按钮制作全局主题。从语义上讲,这是错误的。我们必须手动指定背景和样式,而不是让我们修改默认主题。
  • 我试过了,除了 textColor 一切正常。它只是不继承白色,而是显示黑色。所有其他的都是从样式和背景中正确继承的。如果我在按钮实例上明确设置相同的属性,它就可以工作。很奇怪。
【解决方案2】:

这将更改整个应用程序的默认按钮样式,包括警报对话框按钮。根据你的风格调整风格。

res/values/styles.xml

<resources>

    <!-- Your base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Other styles -->
        <item name="buttonStyle">@style/MyCustomButton</item>
    </style>

    <style name="MyCustomButton" parent="Widget.AppCompat.Button">
        <item name="android:background">@drawable/selector_button</item>
        <item name="android:textColor">@color/buttonText</item>
        <item name="android:textStyle">bold</item>
        <item name="android:paddingLeft">16dp</item>
        <item name="android:paddingRight">16dp</item>
    </style>
</resources>

res/drawable/selector_button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/button_selected" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_selected" android:state_focused="true"/>
    <item android:drawable="@drawable/button_unselected"/>
</selector>

res/drawable/button_selected.xml

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

    <solid android:color="@color/buttonSelected"/>
</shape>

res/drawable/button_unselected.xml

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

    <solid android:color="@color/buttonUnselected"/>
</shape>

【讨论】:

  • 如何在按钮中申请?
  • @AdityaVyas-Lakhan 您可以在 XML 中使用 style="@style/selector_button",如果您以编程方式创建按钮,则可以使用 Button(ContextThemeWrapper(context, R.style.selector_button))
猜你喜欢
  • 1970-01-01
  • 2015-01-11
  • 1970-01-01
  • 2015-04-16
  • 2012-11-24
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
相关资源
最近更新 更多