繁体   English   中英

Android XML更改渐变颜色

[英]android XML change gradient color

我需要在运行时更改XML文件中的android:color

我将其用作图像按钮源

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

我的XML画图文件看起来像这样

<?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>

我尝试了DOM解析器,但是在进行渐变之后,它表明不再有节点

这样尝试。 希望对您有帮助。 谢谢

创建一个新的xml文件并将其放在drawable中,然后将其添加到按钮作为背景

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

您将背景设置为透明,按钮将永远不会有背景。 而是将其设置为您的XML drawabale资源。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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