Android 如何设置按钮的背景颜色

use*_*772 6 android android-button kotlin material-components material-components-android

我正在尝试更改按钮的背景颜色。我在模拟器上使用 SDK 21 的 Kotlin。

View 和 Button 在布局 XML 文件中声明

<View
    android:id="@+id/myview"
    android:layout_width="64dp"
    android:layout_height="32dp"
    />
<Button
    android:id="@+id/showButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="12dp"
    android:text="test"
    />
Run Code Online (Sandbox Code Playgroud)

设置颜色的 API 似乎不起作用:

    showButton.setBackgroundColor(0xff60a0e0.toInt()) <-- doesnt work
Run Code Online (Sandbox Code Playgroud)

有效的是:

    myview.setBackgroundColor(0xff60a0e0.toInt()) <-- works, exact background color
    showButton.setTextColor(0xff000050.toInt()) <-- works, exact text color
Run Code Online (Sandbox Code Playgroud)

经过进一步尝试后,我似乎只能设置按钮的 Alpha 通道,而不能设置颜色:

    setBackgroundColor( 0xff000000.toInt())  <-- works, opaque
    setBackgroundColor( 0x00000000.toInt())  <-- works, transparent
Run Code Online (Sandbox Code Playgroud)

同样的事情还有:

        showButton.setBackgroundColor(Color.GREEN) <-- doesnt work, button is opaque but not green
        showButton.setBackgroundColor(Color.TRANSPARENT) <-- works, button is transparent
Run Code Online (Sandbox Code Playgroud)

任何想法?我是否错过了其他答案或文档中的某些内容?

这是完整的布局,它用于填充片段(如果重要的话):



    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
           <View
               android:id="@+id/myview"
               android:layout_width="64dp"
               android:layout_height="32dp"
              />
           <Button
               android:id="@+id/showButton"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="12dp"
               android:text="test"
               />
        </LinearLayout>
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/dictionaryEntryRecyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layoutManager="LinearLayoutManager"
            />
       </LinearLayout>

Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 1

由于您使用的是Theme.MaterialComponents.Light.DarkActionBar主题,请检查文档并仅使用MaterialButtonwithapp:backgroundTint属性:

<com.google.android.material.button.MaterialButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:backgroundTint="@color/color_selector"
    android:textColor="#FFF"
    android:text="BUTTON"
    />
Run Code Online (Sandbox Code Playgroud)

其中 color_selector 可以是颜色或选择器。就像是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="@color/..." android:state_enabled="true"/>
  <item android:alpha="0.12" android:color="@color/..."/>
</selector>
Run Code Online (Sandbox Code Playgroud)

两个按钮的屏幕截图 - 一个粉色,一个蓝色