简体   繁体   中英

Button color not change... ( Android)

I was working on a project I can create a round button and set the gradient color but when I use it in Button background the color not changed.

rounded_shape.xml

<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"   >

    <gradient
        android:startColor="#9bbbc1"
        android:centerColor="#456268"
        android:endColor="#456268"
        android:angle="270" />

    <corners
        android:radius="25dp"   >
    </corners>



</shape>

Button in Xml

 <Button
            android:id="@+id/btn1"
            android:layout_below="@+id/edttxt1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80dp"
            android:layout_marginTop="2dp"
            android:layout_marginRight="80dp"
            android:layout_marginBottom="2dp"
            android:background="@drawable/rounded_shape"
            android:padding="12dp"
            android:paddingTop="2dp"
            android:paddingEnd="2dp"
            android:paddingBottom="4dp"
            android:text="Analyse"
            android:textAllCaps="false"
            android:textColor="@android:color/white"
            android:textSize="20dp"
            android:textStyle="italic"></Button>

Try it in rounded_shape.xml :

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="12dip" />
            <stroke android:width="1dip" android:color="#333333" />
            <gradient android:angle="-90" android:startColor="#333333"      android:endColor="#555555"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="12dip" />
            <stroke android:width="1dip" android:color="#333333" />
            <solid android:color="#58857e"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="12dip" />
            <stroke android:width="1dip" android:color="#333333" />
            <gradient android:angle="-90" android:startColor="#333333" android:endColor="#555555" />
        </shape>
    </item>

</selector>

The AppCompat library helps with bringing modern theming to your app on all Android versions, so change Button to androidx.appcompat.widget.AppCompatButton:

<androidx.appcompat.widget.AppCompatButton
        android:id="@+id/btn1"
        android:layout_below="@+id/edttxt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="80dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="80dp"
        android:layout_marginBottom="2dp"
        android:background="@drawable/rounded_shape"
        android:padding="12dp"
        android:paddingTop="2dp"
        android:paddingEnd="2dp"
        android:paddingBottom="4dp"
        android:text="Analyse"
        android:textAllCaps="false"
        android:textColor="@android:color/white"
        android:textSize="20dp"
        android:textStyle="italic”/>

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