android动态设置边框颜色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:color="#d6d6d6"
        android:width="1dp"/>//设置边框的颜色和宽度

//设置边框的圆角 <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"/>
<solid android:color="#f75830" ></solid>//设置边框的填充色
</shape>

1.动态改变边框的颜色:

spaceTV = (TextView)findViewById(R.id.space);//spaceTV需要改变边框的控件
GradientDrawable drawable = (GradientDrawable) spaceTV.getBackground();
drawable.setStroke(1, Color.parseColor("#ff0000"));//设置边框的宽度和颜色
如果使用自己定义的颜色请使用drawable.setColor(getResources().getColor(R.color.yellow_color));
设置填充色

drawable.setColor( Color.parseColor("#ff495b"));

事情该步骤的前提:
compile 'com.android.support:design:25.+'
不然会有一堆的错误
原文地址:https://www.cnblogs.com/hualuoshuijia/p/7792084.html