1.关于自定义的背景显示,本来是选择动态添加背景图片,但是需要判断的情况有三十几种,自定义多种背景或根据状态判断再显示图片显然都十分麻烦,感觉系显示的风格也很不好。网络上找了一下相关的文章,把自己的解决方案贴出来。
1)自定义一个圆角背景 weather_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <!--填充色--> <solid android:color="@android:color/white" /> <!--边框颜色及宽度--> <stroke android:color="@color/product_text_color" android:width="2dp"/> <corners android:topLeftRadius="6dp" android:topRightRadius="6dp" android:bottomRightRadius="6dp" android:bottomLeftRadius="6dp"/> </shape>
2)在代码中根据需求动态的修改填充色及背景色
//tvBind是我的textview控件,将边线的颜色变成红色,边线宽度2dp
GradientDrawable drawable = (GradientDrawable) tvBind.getBackground(); drawable.setStroke(2, getResources().getColor(R.color.mall_red_text));
//改变drawable的背景填充色
drawable.setColor(getResources().getColor(R.color.mall_red_text));
3)上传一下我的效果图
根据后台返回的色值动态修改边框和文字背景色。
代码如下:
int color = Color.parseColor(item.getColorCode()); GradientDrawable drawable = (GradientDrawable) llIndex.getBackground(); drawable.setStroke(4, color); GradientDrawable drawable2 = (GradientDrawable) tvIndexState.getBackground(); drawable2.setStroke(4, color); drawable2.setColor(color);
4)stackoverflow上的解决方案
http://stackoverflow.com/questions/16775891/how-to-change-solid-color-from-the-code