如何在安卓中更改按钮的背景?

huangapple go评论66阅读模式
英文:

How do I change the background of a button in android?

问题

这是按钮的当前外观:

如何在安卓中更改按钮的背景?

android:background="@color/white" 并没有改变任何东西。

代码:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="20dp"
    android:text="Button"
    android:background="@color/white"/>
英文:

This is how the button looks right now:

如何在安卓中更改按钮的背景?

android:background= &quot;@color/white&quot; does not change anything.

Code:

 &lt;Button
            android:id=&quot;@+id/button&quot;
            android:layout_width=&quot;wrap_content&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginLeft=&quot;20dp&quot;
            android:layout_marginTop=&quot;20dp&quot;
            android:text=&quot;Button&quot;
            android:background=&quot;@color/white&quot;/&gt;

答案1

得分: 1

使用 app:backgroundTint=&quot;#FFFFFF&quot;

如果您想要使用选择器,您需要将 android:background=&quot;@drawable/your_button_selector_id&quot; 作为按钮的背景,以使选择器起作用。

选择器:

<selector xmlns:android="schemas.android.com/apk/res/android">
    <item android:drawable="@color/white" android:state_selected="true" />
    <item android:drawable="@color/black" android:state_selected="false" />
</selector>
英文:

Use app:backgroundTint=&quot;#FFFFFF&quot;

If you want to use a selector, you will need to have android:background=&quot;@drawable/your_button_selector_id&quot; as the button background for the selector to work.

Selector:

   &lt;selector xmlns:android=&quot;schemas.android.com/apk/res/android&quot;&gt;

   &lt;item android:drawable=&quot;@color/white&quot; android:state_selected=&quot;true&quot; /&gt;

 &lt;item android:drawable=&quot;@color/black&quot; android:state_selected=&quot;false&quot; /&gt; &lt;/selector&gt;

答案2

得分: 1

由于您正在使用颜色,只需使用 **`app:backgroundTint`** 属性:

    <Button
        app:backgroundTint="@color/white"/>

如果您只需要带有描边的圆角,无需使用可绘制对象。

请使用以下方式:

    <com.google.android.material.button.MaterialButton
        app:cornerRadius="16dp"
        app:backgroundTint="@color/white"
        app:strokeColor="@color/black"
        android:textColor="@color/black"
        app:strokeWidth="2dp" />

[![在这里输入图片描述][1]][1]

  [1]: https://i.stack.imgur.com/HTy1D.png
英文:

Since you are using a color just use the app:backgroundTint attribute:

&lt;Button
    app:backgroundTint=&quot;@color/white&quot;/&gt;

If you just need rounded corners with a stroke, you don't need a drawable.

For it use:

    &lt;com.google.android.material.button.MaterialButton
        app:cornerRadius=&quot;16dp&quot;
        app:backgroundTint=&quot;@color/white&quot;
        app:strokeColor=&quot;@color/black&quot;
        android:textColor=&quot;@color/black&quot;
        app:strokeWidth=&quot;2dp&quot; /&gt;

如何在安卓中更改按钮的背景?

答案3

得分: 0

你在你的 colors.xml 文件中添加了白色的值吗?
如果没有,请打开 colors.xml 并添加

<color name="white">#FFFFFF</color>

完整的解释在这里
https://stackoverflow.com/a/2749027/7174681

英文:

Did you add value for white in your colors.xml?
if not open colors.xml and add

&lt;color name=&quot;white&quot;&gt;#FFFFFF&lt;/color&gt;

Full Explanation here
https://stackoverflow.com/a/2749027/7174681

huangapple
  • 本文由 发表于 2020年10月24日 20:57:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64513538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定