在使用shape的同时,用代码修改shape的颜色属性

原文地址为: 在使用shape的同时,用代码修改shape的颜色属性

Android里面经常会使用shape来定制一些View的背景

可以修改View的背景颜色,形状等属性


一般情况下,shape都是在xml文件里面写死了,今天遇到一个需求,View的形状是圆角的,但是颜色是在代码里面设置的

最开始的思路是先在代码里给View设置颜色,再在shape里面设置solid属性为透明色

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners
        android:topLeftRadius="8dp"
        android:topRightRadius="8dp"
        android:bottomLeftRadius="8dp"
        android:bottomRightRadius="8dp"/>
    
    <solid
        android:color="#00000000"/>

</shape>

View.setBackgroundColor(color);
View.setBackgroundDrawable(R.drawable.shape);

很遗憾,不能实现我想要的需求,每设置一次Background,Background就会就会被替换掉


最后上网Google了下,找到解决方案了http://stackoverflow.com/questions/16775891/how-to-change-solid-color-from-the-code

GradientDrawable myGrad = (GradientDrawable)view.getBackground();
myGrad.setColor(color);



转载请注明本文地址: 在使用shape的同时,用代码修改shape的颜色属性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值