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

Android how to set background color on a Button

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

布局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"
    />

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

    showButton.setBackgroundColor(0xff60a0e0.toInt()) <-- doesnt work

有效的是:

    myview.setBackgroundColor(0xff60a0e0.toInt()) <-- works, exact background color
    showButton.setTextColor(0xff000050.toInt()) <-- works, exact text color

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

    setBackgroundColor( 0xff000000.toInt())  <-- works, opaque
    setBackgroundColor( 0x00000000.toInt())  <-- works, transparent

同样的事情还有:

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

有什么想法吗?我是否遗漏了其他答案或文档中的内容?

这是完整的布局,它曾经用来膨胀一个片段,如果重要的话:



    <?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>

mButton.setBackgroundColor(ContextCompat.getColor(mContext, R.color.xxx));

您可以通过两种方式更改颜色;通过 XML 或通过编码。我会推荐 XML,因为初学者更容易理解。

xml 添加此属性以设置背景颜色 android:background="#000"

 <Button
            android:id="@+id/showButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="fgkdjgdjsf"
            android:background="#000"
            />

编码:

showButton.setBackgroundColor(resources.getColor(R.color.colorPrimary))
        showButton.setBackgroundColor(Color.BLACK)

在您的布局中,您正在使用

 <Button
           android:id="@+id/showButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="12dp"
           android:text="test"
           />

如果你想在Button中设置textSize,你应该使用

android:textSize="12dp" 

对于在按钮中设置的背景,您的布局应如下所示:-

 <Button
    android:id="@+id/showButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:text="test"
    android:background="#ff60a0e0"/>

或者您也可以将 colors.xml 中的颜色设置为 :-

<color name="button_background">#ff60a0e0</color>

然后您的布局中的按钮标签将显示为

 <Button
           android:id="@+id/showButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:textSize="12dp"
           android:text="test"
           android:background="@color/button_background"/>

动态你可以设置颜色为

showButton.setBackgroundColor(ContextCompat.getColor(context!!, R.color.button_background))

由于您使用的是 Theme.MaterialComponents.Light.DarkActionBar 主题,请检查 doc 并仅使用 MaterialButtonapp: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"
    />

其中 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>

现在在 kotlin(androidx) 中你可以使用它,

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/buttonExploreAll"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAllCaps="false"
    android:background="@drawable/select_yellow"
    android:text="button"
    android:textColor="@color/white"
    />

和select_yellow是你的风格xml。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:color="@color/colorYellow"
        android:width="@dimen/_1sdp"/>

    <corners
        android:radius="@dimen/_200sdp" />

    <solid android:color="@color/colorYellow" />

</shape>

我使用了“backgroundTint”来改变颜色,点击后颜色没有改变。当我通过“背景”按钮更改颜色时,问题就解决了。