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

6

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

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


请粘贴完整的 XML 布局和样式(如果有使用)。 - Elias Fazel
我已经粘贴了布局,谢谢! - user3144772
1
在Android中,设置颜色的某些方法调用实际上需要一个“资源”ID(int),而不是表示颜色的实际int。 - David
我看到的是我可以设置透明度: setBackgroundColor(0xff000000.toInt())但不能改变颜色: setBackgroundColor(0xff00ff00.toInt()) <-- 不会改变颜色(变成绿色) - user3144772
@GabrieleMariotti 是的,尽管我不知道(我是从Android Studio导入示例开始的应用程序)在我的styles.xml中有: <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">按钮颜色取自colors.xml中的“colorAccent”所以现在我的问题是我无法使按钮使用除“colorAccent”之外的其他颜色感谢,这很有帮助! - user3144772
显示剩余2条评论
6个回答

3

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

可以用来设置按钮的背景颜色,其中 ContextCompat.getColor(mContext, R.color.xxx) 用于获取xxx颜色在当前上下文中的int值。


1
你可以通过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"
            />

Coding:

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

1
在你的布局中,你正在使用


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

如果您想在按钮中设置文本大小,您应该使用

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

感谢您详细的答案。但正如我在问题中指出的那样,这不起作用。并且正如 @Gabriele Mariotti 指出的那样,这是因为我正在使用 Material 主题。 - user3144772
对我来说,颜色是存储在 SQL 中的(并通过互联网发送到手机)。因此,颜色只能动态设置为整数/字符串。必须有一种方法来设置颜色。 - Bart Mensfort

1

由于您正在使用Theme.MaterialComponents.Light.DarkActionBar主题,请查看文档,并仅使用带有app:backgroundTint属性的MaterialButton

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

Screenshot of two buttons - one pink, one blue


是的,这个可以运行。谢谢。通常的答案(setBackgroudColor)在Material主题下不起作用... - user3144772

0
我使用了“backgroundTint”来改变颜色,但是在点击后颜色没有改变。当我通过“background”更改按钮的颜色时,问题得到了解决。

0

现在在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>

This look like


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接