简体   繁体   中英

android XML change gradient color

i need to change android:color in XML file in run-time

i use it for an image button source

<ImageButton 
            android:layout_width="@dimen/large_brush"
            android:layout_height="@dimen/large_brush"
            android:background="@android:color/transparent"
            android:contentDescription="@string/paint"
            android:src="@drawable/paint"/>

my XML paint file looks like this

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

<item>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval" >
        <stroke
            android:width="1dp"
            android:color="@android:color/holo_purple" />

        <gradient
            android:startColor="@android:color/holo_purple"
            android:endColor="@android:color/holo_purple" />

        <corners android:radius="10dp" />
    </shape>
</item>

</layer-list>

i tried DOM parser but after getting to gradient it shows that there are no more nodes

Try like this way. Hope it should helpful for you. Thanks

Create a new xml file and put it in drawable and then add it to button as background

gradient.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!--  Gradient Bg for listrow -->
<gradient
  android:startColor="#f1f1f2"
  android:centerColor="#e7e7e8"
  android:endColor="#cfcfcf"
  android:angle="270" />
</shape>

layout.xml

<Button
android:id="@+id/Button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="@drawable/gradient"
android:text="Button Text" >

You have the background set to transparent, the button will never have a background. Set it to you XML drawabale resource instead.

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