为什么按钮的背景不会改变

时间:2017-08-26 22:03:51

标签: android user-interface colors widget styles

我正在尝试创建一种全局样式,其中我的按钮具有带有白色文本的深灰色背景。文字颜色很好。我能够改变它,但由于某种原因,按钮的背景颜色不会改变。我究竟做错了什么。

这是我的颜色xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#bf360c</color>
    <color name="colorPrimaryLight">#f9683a</color>
    <color name="colorPrimaryDark">#870000</color>
    <color name="colorPrimaryText">#000000</color>
    <color name="colorAccent">#a4a4a4</color>
    <color name="colorWindowBackground">#fafafa</color>

    <color name="RED">#ff0000</color>
    <color name="BLUE">#000fff</color>
    <color name="GREEN">#00FF00</color>
    <color name="YELLOW">#FFEB3B</color>
    <color name="DARK_GREY">#494949</color>
</resources>

这是我的样式xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="@style/Theme.AppCompat">
        <!-- 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:textColor">@color/colorPrimaryText</item>
        <item name="android:textColorSecondary">@color/colorAccent</item>
        <item name="android:windowBackground">@color/colorWindowBackground</item>
        <item name="android:buttonStyle">@style/buttonColor</item>

    </style>

    <style name="buttonColor" parent="android:style/Widget.Button">
        <item name="android:textColor">@color/colorWindowBackground</item>
        <item name="android:colorBackground">@color/DARK_GREY</item>
    </style>

</resources>

为什么我的退出按钮会突然缩小? enter image description here

现在我有了这个,但退出按钮缩小了??????

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="16dp"
    tools:context="com.checkinsystems.ez_score.MainActivity">

    <Button
        android:id="@+id/match_button"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:onClick="goToMatchScreen"
        android:text="@string/match_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/competitors_button"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_marginTop="15dp"
        android:onClick="goToCompetitorsScreen"
        android:text="@string/competitors_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/match_button"/>

    <Button
        android:id="@+id/scores_button"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_marginTop="15dp"
        android:onClick="goToScoresScreen"
        android:text="@string/scores_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/competitors_button"/>

    <Button
        android:id="@+id/reports_button"
        android:layout_width="0dp"
        android:layout_height="65dp"
        android:layout_marginTop="15dp"
        android:onClick="goToReportsActivity"
        android:text="@string/reports_button"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scores_button"/>

    <Button
        android:id="@+id/exit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/exit_button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"/>

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:0)

如何设置按钮的背景?

android:background=""

答案 1 :(得分:0)

使用

 <item name="colorButtonNormal">@color/yourColor</item>

而不是

 <item name="android:colorBackground">@color/DARK_GREY</item>`.

答案 2 :(得分:0)

关于按钮退出的第二个问题,请尝试从“wrap_content”更改按钮的宽度和高度

...
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...