【android】 使用一个shape.xml文件,使用代码设置不同圆角背景颜色

   给一个View设置一个圆角的背景颜色,我们一般会使用xml文件设置,使用<shape>节点设置,但是如果我们对一系列的View设置圆角北京,并且背景颜色色值不同,那么我们第一感觉想到的是创建多个xml文件,更改solid填充背景,其实我们可以使用一个xml文件就可以搞定,使用代码更改里面的填充颜色色值。废话不多话,看代码。

        效果:

 首先:创建activity_main.xml

     

[java]  view plain copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <GridView  
  7.         android:id="@+id/gv_gridview"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="match_parent"  
  10.         android:horizontalSpacing="10dp"  
  11.         android:numColumns="4"  
  12.         android:verticalSpacing="10dp" >  
  13.     </GridView>  
  14.   
  15. </RelativeLayout>  

MainActivity.java

[java]  view plain copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private GridView gdview;  
  4.     private Context mContext;  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_main);  
  10.         mContext = MainActivity.this;  
  11.         gdview = (GridView) findViewById(R.id.gv_gridview);  
  12.         initDate();  
  13.     }  
  14.   
  15.     private void initDate() {  
  16.         List<String> lists = new ArrayList<String>();  
  17.         lists.add("颜色1");  
  18.         lists.add("颜色2");  
  19.         lists.add("颜色3");  
  20.         lists.add("颜色4");  
  21.         lists.add("颜色5");  
  22.         lists.add("颜色6");  
  23.         lists.add("颜色7");  
  24.         lists.add("颜色8");  
  25.         lists.add("颜色9");  
  26.         lists.add("颜色10");  
  27.         lists.add("颜色11");  
  28.         lists.add("颜色12");  
  29.         gdview.setAdapter(new MyGridViewAdapter(mContext, lists));  
  30.     }  
  31.   
  32. }  

下面新建自定义适配器MyGridViewAdapter.java

[java]  view plain copy
  1. public class MyGridViewAdapter extends BaseAdapter {  
  2.   
  3.     private Context mContext;  
  4.     private List<String> data;// 显示的数据  
  5.     private int[] colors = { R.color.item1, R.color.item2, R.color.item3,  
  6.             R.color.item4, R.color.item5, R.color.item6, R.color.item7,  
  7.             R.color.item8, R.color.item9, R.color.item10, R.color.item11,  
  8.             R.color.item12 };// 颜色色值id数组  
  9.   
  10.     public MyGridViewAdapter(Context mContext, List<String> data) {  
  11.         super();  
  12.         this.mContext = mContext;  
  13.         this.data = data;  
  14.     }  
  15.   
  16.     @Override  
  17.     public int getCount() {  
  18.         if (data == null) {  
  19.             return 0;  
  20.         }  
  21.         return data.size();  
  22.     }  
  23.   
  24.     @Override  
  25.     public Object getItem(int position) {  
  26.         // TODO Auto-generated method stub  
  27.         return data.get(position);  
  28.     }  
  29.   
  30.     @Override  
  31.     public long getItemId(int position) {  
  32.         // TODO Auto-generated method stub  
  33.         return position;  
  34.     }  
  35.   
  36.     @Override  
  37.     public View getView(int position, View convertView, ViewGroup parent) {  
  38.         ViewHolder holder = null;  
  39.         if (convertView == null) {  
  40.             holder = new ViewHolder();  
  41.             convertView = View.inflate(mContext, R.layout.item_griview, null);  
  42.             holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item);  
  43.             holder.tv_item.setBackgroundResource(R.drawable.circle);  
  44.             convertView.setTag(holder);  
  45.         } else {  
  46.             holder = (ViewHolder) convertView.getTag();  
  47.         }  
  48.         String item = (String) getItem(position);  
  49.         holder.tv_item.setText(item);  
  50.         // 获取背景颜色,并且改变颜色  
  51.         GradientDrawable myGrad = (GradientDrawable) holder.tv_item  
  52.                 .getBackground();  
  53.         myGrad.setColor(mContext.getResources().getColor(getColor(position)));  
  54.         return convertView;  
  55.     }  
  56.   
  57.     /** 
  58.      *  
  59.      * @方法名称:getColor 
  60.      * @描述: TODO 
  61.      * @创建人:yang 
  62.      * @创建时间:2014年9月30日 下午3:06:04 
  63.      * @备注:获取背景的颜色的色值 
  64.      * @param position 
  65.      * @return 
  66.      * @返回类型:int 
  67.      */  
  68.     public int getColor(int position) {  
  69.         if (position < colors.length) {  
  70.             return colors[position];  
  71.         } else {  
  72.             return colors[position % colors.length];  
  73.         }  
  74.     }  
  75.   
  76.     class ViewHolder {  
  77.         TextView tv_item;  
  78.     }  
  79. }  

item_griview.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/tv_item"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="match_parent"  
  11.         android:gravity="center"  
  12.         android:paddingBottom="5dp"  
  13.         android:paddingLeft="10dp"  
  14.         android:paddingRight="10dp"  
  15.         android:paddingTop="5dp" />  
  16.   
  17. </LinearLayout>  

提前在res/vlaues/color.xml中增加背景颜色色值

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <color name="item1">#eed776</color>  
  4.     <color name="item2">#f2a96b</color>  
  5.     <color name="item3">#7fda54</color>  
  6.     <color name="item4">#aaaae1</color>  
  7.     <color name="item5">#d785de</color>  
  8.     <color name="item6">#a8a8e2</color>  
  9.     <color name="item7">#91d56c</color>  
  10.     <color name="item8">#d4ac65</color>  
  11.     <color name="item9">#7ac7c6</color>  
  12.     <color name="item10">#f09292</color>  
  13.     <color name="item11">#83b4e4</color>  
  14.     <color name="item12">#d7d87e</color>  
  15. </resources>  


如果想显示的背景的个数,背景颜色的色值和内容,可以在values中做相应的变化。


源码下载地址http://download.csdn.net/detail/forwardyzk/7993463


转自:http://blog.csdn.net/forwardyzk/article/details/39696331

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值