Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在程序中用xml定义颜色_Android - Fatal编程技术网

Android 如何在程序中用xml定义颜色

Android 如何在程序中用xml定义颜色,android,Android,我在Res\color文件夹中定义了渐变色xml(green\u gradient.xml)。 如何在自定义视图中将其绘制到条形图上。对于普通的绿色,我喜欢画画 paint.setColor(Color.GREEN); 但是如何用Res\Color文件夹中定义的GREEN\u gradient.xml替换Color.GREEN <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://sche

我在Res\color文件夹中定义了渐变色xml(green\u gradient.xml)。 如何在自定义
视图
中将其绘制到条形图上。对于普通的绿色,我喜欢画画

paint.setColor(Color.GREEN);
但是如何用Res\Color文件夹中定义的
GREEN\u gradient.xml
替换
Color.GREEN

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
编辑1。 我的green_gradient.xml位于res\color文件夹中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>

假设您在自定义颜色集中定义了绿色,您可以这样做

<your_context>.getResources().getColor(R.color.GREEN);

drawable
文件夹中设置
green\u gradient.xml
,然后您可以使用
R.drawable.green\u gradient
以编程方式访问它。在布局xml文件中,您可以通过
@drawable/green\u gradient

向我们显示
green\u gradient.xml
您有
colors.xml
,在该文件下您有资源名
green\u gradient
?。需要更多信息。不,我有Res\Color中的green_gradient.xml。我喜欢使用green_gradient.xml在自定义视图中绘制我的工具栏。对于xml,我可以很容易地将其作为android:background=“@color/green_gradient”。但是如何在程序中获得它。op说他有这个
green\u gradient.xml
而不是
colors.xm
colors.xml
就是一个例子。他应该用自己的xml文件名(
green\u gradient.xml
)替换它,并且op说
Res\Color
。我不认为你的例子是op所要求的。是的,我的绿色梯度是Res\Color。xml显示在上面编辑的版本中。但是,由于您正在将颜色设置为绘制,因此我没有看到任何将ColorStateList作为输入的方法。我无法设置ColorStateList颜色;绘制。设置颜色(颜色);我有编译错误。所以你的意思是,自定义视图中的绘图不能使用xml设计的颜色。我的目的是获得渐变颜色。我想我必须在程序中为自定义视图重新设计渐变颜色。不,在酒吧。android:background是我刚才介绍的一个简单的xml示例。我在视图上画了条,想画渐变色。