英文:
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= "@color/white"
does not change anything.
Code:
<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"/>
答案1
得分: 1
使用 app:backgroundTint="#FFFFFF"
如果您想要使用选择器,您需要将 android:background="@drawable/your_button_selector_id"
作为按钮的背景,以使选择器起作用。
选择器:
<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="#FFFFFF"
If you want to use a selector, you will need to have android:background="@drawable/your_button_selector_id"
as the button background for the selector to work.
Selector:
<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>
答案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:
<Button
app:backgroundTint="@color/white"/>
If you just need rounded corners with a stroke, you don't need a drawable.
For it use:
<com.google.android.material.button.MaterialButton
app:cornerRadius="16dp"
app:backgroundTint="@color/white"
app:strokeColor="@color/black"
android:textColor="@color/black"
app:strokeWidth="2dp" />
答案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
<color name="white">#FFFFFF</color>
Full Explanation here
https://stackoverflow.com/a/2749027/7174681
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论