如何动态改变笔触颜色?

时间:2014-01-30 10:56:29

标签: android android-studio shape

(抱歉我的语言,我是法国人)

我需要更改Shape的stroke color。 我遇到的问题是here。 当EditText不正确时,我需要更改纯色。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" android:padding="10dp">
    <solid android:color="#FFFFFF"/>
    <corners
        android:bottomRightRadius="5dp"
        android:bottomLeftRadius="5dp"
        android:topLeftRadius="5dp"
        android:topRightRadius="5dp"/>
    <stroke android:width="2px" android:color="#CCCCCC"/>
</shape>

答案:

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setColor(Color.BLACK);

答案中的问题是,我不理解矩形项。 如果我用EditText替换它,它将应用于背景,而不是固体背景。

提前致谢。

编辑:我的错,我想改变 Stroke 颜色,而不是固体。

1 个答案:

答案 0 :(得分:16)

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setStroke(2, Color.RED);