简体   繁体   中英

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

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

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>

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM