【问题标题】:Android cannot set default button styleAndroid 无法设置默认按钮样式
【发布时间】:2015-05-26 12:13:55
【问题描述】:

我正在尝试为我的应用程序中的所有按钮设置默认背景。问题是android:buttonStyle 项目没有接缝做任何事情。

如果我为任何按钮设置@style/Button 样式,则使用该样式,但我希望这发生在所有没有明确提供样式的按钮上。

我的themes.xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style
        name="BBTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">

        <item name="android:windowBackground">@drawable/app_background</item>
        <item name="android:actionBarStyle">@style/ActionBar</item>
        <item name="android:buttonStyle">@style/Button</item>
    </style>
</resources>

而我的styles.xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- ActionBar styles -->
    <style
        name="ActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">

        <item name="android:background">@color/action_bar_background</item>
    </style>

    <!-- Button styles -->
    <style
        name="Button"
        parent="@android:style/Widget.Button">

        <item name="android:textColor">@android:color/white</item>
        <item name="android:background">@drawable/button_background</item>
    </style>

</resources>

【问题讨论】:

  • 同样的问题...有什么解决办法吗?

标签: android android-theme android-styles


【解决方案1】:

在你的drawable文件夹中添加buttonstyles.xml,调用From Button,你可以修改按钮的样式。

<Button
                            android:id="@+id/button6"
                            style="?android:attr/buttonStyleSmall"
                            android:layout_width="140dp"
                            android:layout_height="wrap_content"
                            android:layout_marginBottom="15dp"
                            android:layout_marginLeft="10dp"
                            android:layout_marginRight="10dp"
                            android:layout_marginTop="15dp"
                            android:background="@drawable/buttonstyles"
                            android:text="Cancel"
                            android:textColor="#fff" />

buttonstyles.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#99d9fa"
        android:endColor="#99d9fa"
        android:angle="45"/>
    <padding android:left="3dp"
        android:top="3dp"
        android:right="3dp"
        android:bottom="3dp" />
    <corners android:radius="15dp" />

</shape>

【讨论】:

  • 正如我在问题中指定的那样,我不想每次使用Button 时都设置样式,我想为所有按钮设置默认样式。
  • 检查这个链接,它可能对你有用-androidcookbook.com/Recipe.seam?recipeId=3307
  • 我经历了这一切,这正是我正在做的事情,但没有任何结果。我正在以我的风格扩展 Widget.Button 并设置背景图像,但除非我将该风格直接设置为 Button,否则该图像永远不会显示 - 默认情况下不像我想要的那样。
猜你喜欢
  • 2012-10-24
  • 1970-01-01
  • 2018-03-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
相关资源
最近更新 更多