How to change stroke color dynamically?

南楼画角 提交于 2019-12-04 22:32:08

问题


(Sorry for my language, I'm french)

I need to change the stroke color of a Shape. I've the same problem described here. I need to change solid color when the EditText is not correct.

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

The answer :

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

Problem in the answer is, I don't understand the rectangle item. If I replace it by the EditText, it is applied to the background, not the solid background.

Thanks in advance.

EDIT : My Bad, i want to change the Stroke color, not solid.


回答1:


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


来源:https://stackoverflow.com/questions/21454474/how-to-change-stroke-color-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!