为什么android:buttonStyle不能为按钮设置样式? [英] Why android:buttonStyle does not style buttons?

查看:825
本文介绍了为什么android:buttonStyle不能为按钮设置样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个奇怪的问题:我的应用程序非常简单,主要活动只有一个按钮,并且清单中设置了活动的自定义主题.

So, I have this weird issue: My app is very simple, main activity with just one button in it and custom theme for the activity set in the manifest.

我可以确认主题已被选中,因为我可以更改活动背景或字体颜色,例如

I can confirm the theme works and is selected, as I can change activity background or font color e.g.

但是当我尝试为活动中的所有按钮设置样式时,它将无法正常工作!

But when I try to style all Buttons on my activity, it does not work!

这是我使用的 styles.xml 代码:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:background">#0f0</item>
        <item name="android:buttonStyle">@style/btxs</item>
    </style>

    <style name="btxs" parent="@android:style/Widget.Button">
        <item name="android:textColor">#f00</item>
    </style>

</resources>

活动背景为绿色(因此主题有效),但按钮仍为默认设置.

Background of activity is green (so theme works), but button is still default.

android:buttonStyle在主题中设置按钮样式的正确方法吗?

Is android:buttonStyle correct way of setting button style in theme?

供参考,我还粘贴了 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginTop="25dp" />
</RelativeLayout>

推荐答案

AppCompatActivity将使用buttonStyle.我还假设您可能想扩展Base.Widget.AppCompat.Button.

An AppCompatActivity will use buttonStyle. I'm also assuming that you probably want to extend Base.Widget.AppCompat.Button.

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:background">#0f0</item>
        <item name="buttonStyle">@style/btxs</item>
    </style>

    <style name="btxs" parent="Base.Widget.AppCompat.Button">
        <item name="android:textColor">#f00</item>
    </style>

</resources>

这篇关于为什么android:buttonStyle不能为按钮设置样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆