Button背景颜色不变

Button背景颜色不变

本文介绍了Android Button背景颜色不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此android项目中,我创建了默认按钮样式.我的styles.xml

In this android project im creating a default button style.my styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:buttonStyle">@style/ButtonStlye123</item>
</style>

<style name="ButtonStlye123" parent="android:style/Widget.Button">
    <item name="android:textSize">19dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="android:padding">8dp</item>
    <item name="android:textColor">@color/ColorDivider</item>

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

我的按钮在一个片段中.

my button in a fragment.

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnLogin"
    android:id="@+id/btn_login"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:theme="@style/ButtonStlye123"
    android:onClick="login" />

在android studio中的预览中,它看起来完全像我想要的,但是当我在设备上运行该应用程序时,背景颜色为灰色(默认按钮颜色").如果我更改styles.xml中的文本颜色,例如:

In the preview in android studio it looks exactly like I want, but when I run the app on my device the background color is gray ("default button color"). If i change the text color in my styles.xml like:

<item name="android:textColor">@color/ColorPrimary</item>

它会更改(也在我的设备上),因此正在加载自定义样式,我尝试为按钮尝试使用不同的颜色(适用于文本),但不会更改.

it changes (also on my device) so the custom style is loading, i tryd different colors (that worked for the text) for the button but it wont change.

为什么不加载我的背景色?

Why isnt my background color loading?

推荐答案

我认为您需要将属性从主题"更改为样式".这些文件用于不同的用途,因此使用的方式也不同.

I think you need to change your attribute from "theme" to "style". These files are intended for different things, so are used differently.

样式用于将属性分配给各个视图或组件,主题用于将属性应用到整个应用程序.

Style is used to assign attributes to individual views or components, and Theme is used to apply attributes to the entire app.

因此,请尝试以下操作:

So try this instead:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnLogin"
    android:id="@+id/btn_login"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    <!-- This attribute was changed -->
    android:style="@style/ButtonStlye123"
    android:onClick="login" />

您还应该适当地将这些不同类型的XML拆分为正确的文件(styles.xml和themes.xml-而不是将它们全部保存在同一文件中).这是惯例,不会影响代码的编译或执行,但建议将其作为代码样式.

You should also split these different types of XML into the correct files as appropriate (styles.xml and themes.xml - instead of keeping them all in the same file). This is convention, and won't effect the code compilation or execution, but is recommended as a code style.

这篇关于Android Button背景颜色不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:57