给一个View设置一个圆角的背景颜色,我们一般会使用xml文件设置,使用<shape>节点设置,但是如果我们对一系列的View设置圆角北京,并且背景颜色色值不同,那么我们第一感觉想到的是创建多个xml文件,更改solid填充背景,其实我们可以使用一个xml文件就可以搞定,使用代码更改里面的填充颜色色值。废话不多话,看代码。
效果:
首先:创建activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/gv_gridview" android:layout_width="match_parent" android:layout_height="match_parent" android:horizontalSpacing="10dp" android:numColumns="4" android:verticalSpacing="10dp" > </GridView> </RelativeLayout>
MainActivity.java
public class MainActivity extends Activity { private GridView gdview; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; gdview = (GridView) findViewById(R.id.gv_gridview); initDate(); } private void initDate() { List<String> lists = new ArrayList<String>(); lists.add("颜色1"); lists.add("颜色2"); lists.add("颜色3"); lists.add("颜色4"); lists.add("颜色5"); lists.add("颜色6"); lists.add("颜色7"); lists.add("颜色8"); lists.add("颜色9"); lists.add("颜色10"); lists.add("颜色11"); lists.add("颜色12"); gdview.setAdapter(new MyGridViewAdapter(mContext,lists)); } }
下面新建自定义适配器MyGridViewAdapter.java
public class MyGridViewAdapter extends BaseAdapter { private Context mContext; private List<String> data;// 显示的数据 private int[] colors = { R.color.item1,R.color.item2,R.color.item3,R.color.item4,R.color.item5,R.color.item6,R.color.item7,R.color.item8,R.color.item9,R.color.item10,R.color.item11,R.color.item12 };// 颜色色值id数组 public MyGridViewAdapter(Context mContext,List<String> data) { super(); this.mContext = mContext; this.data = data; } @Override public int getCount() { if (data == null) { return 0; } return data.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return data.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position,View convertView,ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = View.inflate(mContext,R.layout.item_griview,null); holder.tv_item = (TextView) convertView.findViewById(R.id.tv_item); holder.tv_item.setBackgroundResource(R.drawable.circle); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } String item = (String) getItem(position); holder.tv_item.setText(item); // 获取背景颜色,并且改变颜色 GradientDrawable myGrad = (GradientDrawable) holder.tv_item .getBackground(); myGrad.setColor(mContext.getResources().getColor(getColor(position))); return convertView; } /** * * @方法名称:getColor * @描述: TODO * @创建人:yang * @创建时间:2014年9月30日 下午3:06:04 * @备注:获取背景的颜色的色值 * @param position * @return * @返回类型:int */ public int getColor(int position) { if (position < colors.length) { return colors[position]; } else { return colors[position % colors.length]; } } class ViewHolder { TextView tv_item; } }
item_griview.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/tv_item" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:paddingBottom="5dp" android:paddingLeft="10dp" android:paddingRight="10dp" android:paddingTop="5dp" /> </LinearLayout>
提前在res/vlaues/color.xml中增加背景颜色色值
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="item1">#eed776</color> <color name="item2">#f2a96b</color> <color name="item3">#7fda54</color> <color name="item4">#aaaae1</color> <color name="item5">#d785de</color> <color name="item6">#a8a8e2</color> <color name="item7">#91d56c</color> <color name="item8">#d4ac65</color> <color name="item9">#7ac7c6</color> <color name="item10">#f09292</color> <color name="item11">#83b4e4</color> <color name="item12">#d7d87e</color> </resources>
如果想显示的背景的个数,背景颜色的色值和内容,可以在values中做相应的变化。