【问题标题】:Button doesn't show background drawable按钮不显示背景可绘制
【发布时间】:2020-06-19 10:49:48
【问题描述】:

我在编程方面很菜鸟,我一直在学习一些教程,并且我更改了样式或颜色或其他内容,现在我遇到的问题是,当我尝试创建按钮时,它开始于一个特定的颜色,为此,它不允许我设置我以前做过的背景资源。

我怎样才能让按钮显示这个drawable?

这是 xml(尽管这发生在我所有的布局中)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginStart="100dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="100dp"
android:background="@drawable/button_black_background">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/user_profile_image_search"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:layout_marginTop="10dp"
        android:padding="5dp"
        android:src="@drawable/profile"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/user_name_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="User name"
        android:textColor="@color/colorTextPrimary"
        android:textSize="12dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/user_profile_image_search" />

    <TextView
        android:id="@+id/user_full_name_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:text="Full name"
        android:textColor="@color/colorTextPrimary"
        android:textSize="12dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/user_name_search"     />

    <Button
        android:id="@+id/follow_btn_search"
        android:layout_width="90dp"
        android:layout_height="35dp"
        android:background="@drawable/button_black_background"
        android:text="Follow"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:padding="2dp"
            app:layout_constraintTop_toBottomOf="@+id/user_full_name_search" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

这是我要使用的drawable

这是我设置了背景但没有显示的按钮

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_black_background"/>

这些是我的颜色

<resources>
<color name="colorPrimary">#721100</color>
<color name="colorPrimaryDark">#3F0101</color>
<color name="colorAccent">#FF5AB0</color>
<color name="colorTextPrimary">#212121</color>
<color name="colorTextSecondary">#757575</color>
</resources>

这些是我的风格

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>


</style>

【问题讨论】:

  • 请附上你的xml布局
  • 当然,我的朋友,现在 XML 就在那里。请注意,在第一个约束布局中,drawable 确实出现了,但在按钮中却没有。我对此很迷茫:(

标签: android xml button colors styles


【解决方案1】:

在约束布局设置的根目录中,相同的可绘制 .pls 从中删除。

【讨论】:

    【解决方案2】:

    您必须使用 androidx 包中的 Button。这段代码,

    <androidx.appcompat.widget.AppCompatButton
        android:id="@+id/follow_btn_search"
        android:layout_width="90dp"
        android:layout_height="35dp"
        android:background="@drawable/button_black_background"
        android:text="Follow"
        android:textAllCaps="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:padding="2dp"
        app:layout_constraintTop_toBottomOf="@+id/user_full_name_search" />
    

    【讨论】:

    • 是的!这是解决方案,非常感谢。这是否意味着所有“默认”按钮总是使用 Colors.xml 文件?
    • 是的。 AppCompatButton 的优点是可以根据状态设置按钮的外观。