码迷,mamicode.com
首页 > 其他好文 > 详细

shape--用代码修改shape的颜色属性

时间:2016-08-31 20:40:52      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

 

Android里面经常会使用shape来定制一些View的背景,可以修改View的背景颜色,形状等属性

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

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

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <corners  
  4.         android:topLeftRadius="8dp"  
  5.         android:topRightRadius="8dp"  
  6.         android:bottomLeftRadius="8dp"  
  7.         android:bottomRightRadius="8dp"/>  
  8.       
  9.     <solid  
  10.         android:color="#00000000"/>  
  11.   
  12. </shape>  
 
  1. View.setBackgroundColor(color);  
  2. View.setBackgroundDrawable(R.drawable.shape);  


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

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

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

 

 

shape--用代码修改shape的颜色属性

标签:

原文地址:http://www.cnblogs.com/awkflf11/p/5827330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!