简体   繁体   English

自定义样式不适用于 Android 中的按钮

[英]Custom style doesn't work for buttons in Android

I have a project with two themes in res/values/themes.我有一个项目,在 res/values/themes 中有两个主题。 I want to add my own button style.我想添加我自己的按钮样式。 I create values/styles.xml:我创建值/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorAccent</item>
    </style>

    <style name="BeatBoxButton">
        <item name="android:background">@color/dark_blue</item>
    </style>
</resources>

Then I add style attribute to Button :然后我将 style 属性添加到 Button :

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
>
    <data>
        <variable
            name="viewModel"
            type="com.example.beatbox.SoundViewModel"
        />
    </data>
    <Button
        style="@style/BeatBoxButton"
        android:layout_width="match_parent"
        android:layout_height="110dp"
        android:padding="7dp"
        android:layout_margin="7dp"
        android:text="@{viewModel.title}"
        android:onClick="@{() -> viewModel.onButtonClick()}"
        tools:text="@string/sound_item_btn_text"
    />
</layout>

Now I want to see my buttons with dark_blue background.现在我想用深蓝色背景查看我的按钮。 But style doesn't apply.但风格不适用。 Why?为什么? And what I need to do to apply BeatBoxButton style?我需要做什么来应用 BeatBoxButton 样式?

PS: And I have res/values/colors.xml too with PS:我也有 res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>

    <color name="red">#F44336</color>
    <color name="dark_red">#C3352B</color>
    <color name="gray">#607D8B</color>
    <color name="soothing_blue">#0083BF</color>
    <color name="dark_blue">#005A8A</color>
</resources>

You are missing the parent attribute in your style, in addition to this, the background attribute is mean to be used with a drawable (an image, a xml...) because the background of a button is more complex than just a color, what you are looking for is the Tint attribute.您在样式中缺少parent属性,除此之外, background属性意味着与可绘制对象(图像、xml...)一起使用,因为按钮的背景比颜色更复杂,您正在寻找的是Tint属性。

Tint attribute is used to give a color tint to the given button. Tint属性用于为给定按钮提供颜色。

<style name="BeatBoxButton" parent="@android:style/Widget.Button">
        <item name="android:backgroundTint">@color/dark_blue</item>
</style>

Change theme in AndroidManifest to "themeApp" did the trick.将 AndroidManifest 中的主题更改为“themeApp”即可。 And we even don't need parent attribute in our custom style!我们甚至不需要在我们的自定义样式中使用 parent 属性!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM