【问题标题】:How to change style of all the buttons in android studio?如何更改android studio中所有按钮的样式?
【发布时间】:2017-02-20 18:04:32
【问题描述】:

Style.xml 文件(AppTheme 是我的应用程序的主题,它继承了 materiallightdarkactionbar 主题)

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material.Light.DarkActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:colorForeground">@color/holo_orange_light</item>
    <item name="android:buttonStyle">@style/newbuttonstyle</item>
</style>
<style name="newbuttonstyle" parent="@android:style/Widget.Button">
    <item name="android:backgroundTint">#acd9e6</item>
</style>

</resources>

我正在设计一个计算器,这是它的键盘......而不是单独设置所有按钮的背景色调我想使用 style.xml 一次性更改所有按钮的样式...... .我可以这样做吗?

在此我使用的是 buttonStyle,但它仍然无法正常工作

<item name="android:buttonStyle">@style/newbuttonstyle</item>

【问题讨论】:

标签: android


【解决方案1】:

在您的 XML 文件中

<Button
style="@style/MyButtonStyle" 
android:id="@+id/btnSignIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="my button"/>

在 style.xml 文件中添加此代码。

<style name="ButtonStyle" parent="@android:style/Widget.Button">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textSize">15sp</item>
        <item name="android:padding">10dp</item>
        <item name="android:textStyle">bold</item>
        <item name="android:background">@color/colorPrimary</item>
        <item name="android:gravity">center</item>
    </style>

根据您的要求更改样式项目。

【讨论】:

  • 我没有办法一次性定义所有按钮的样式吗??
  • 它为所有按钮赋予样式的最后也是最好的方式。
  • 只是你必须把 style="@style/MyButtonStyle" 放在所有按钮中.. 就是这样
【解决方案2】:
<style name="newbuttonstyle" parent="@android:style/Widget.Button">
    <item name="android:background">#acd9e6</item>
</style>

我在我的应用程序中使用上面的代码并且它工作正常。我认为设置背景而不是 backgroundTint。 可能是它的工作。

检查此链接: ReferenceLink

它在 lolipop 的 tintbackground 中存在一些问题。所以您必须在 lolipop 下方签入。

【讨论】:

  • 这仅在我为每个按钮定义 style="@style/newbuttonstyle" 时才有效....但是 @style/newbuttonstyle 应用主题风格
  • 你的compilesdk版本是多少?
  • 24 是我的 compile sdk 版本
  • 请更改为 21 并检查其工作与否。如果工作正常,那么我们将找到版本 24 问题的解决方案
【解决方案3】:

您可以关注这个问题,希望它对您有用并清除您的样式部分: Android Material Design Button Styles

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-18
    • 2013-04-30
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2021-10-16
    • 2013-11-03
    相关资源
    最近更新 更多