Use a shape. xml file and code to set the background color of different rounded corners

Source: Internet
Author: User
Tags getcolor

Set the background color of a rounded corner for a view. We usually use the XML file and the <shape> node settings. However, if we set a rounded corner for a series of views in Beijing, and the background color and color values are different. The first thing we think of is to create multiple XML files and change solid to fill the background. In fact, we can use an XML file, use the code to change the fill color value. Let's look at the code.

Effect:

First, create 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; @ overrideprotected 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 ("color 1"); lists. add ("color 2"); lists. add ("color 3"); lists. add ("color 4"); lists. add ("color 5"); lists. add ("color 6"); lists. add ("color 7"); lists. add ("color 8"); lists. add ("color 9"); lists. add ("color 10"); lists. add ("color 11"); lists. add ("color 12"); gdview. setadapter (New mygridviewadapter (mcontext, lists ));}}

Create a custom adapter 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}; // color value ID array public mygridviewadapter (context mcontext, list <string> data) {super (); this. mcontext = mcontext; this. data = data ;}@ overridepublic int getcount () {If (Data = NULL) {return 0;} return data. size () ;}@ overridepublic object getitem (INT position) {// todo auto-generated method stubreturn data. get (position) ;}@ overridepublic long getitemid (INT position) {// todo auto-generated method stubreturn position ;}@ overridepublic 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); // obtain the background color and change the color gradientdrawable mygrad = (gradientdrawable) Holder. TV _item.getbackground (); mygrad. setcolor (mcontext. getresources (). getcolor (position); Return convertview;}/***** @ Method Name: getcolor * @ Description: Todo * @ Creator: Yang * @ Creation Time: september 30, 2014 3:06:04 * @ Note: Obtain the color of the background * @ Param position * @ return type: 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>

Add background color values to RES/vlaues/color. XML in advance.

<?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>


If you want to display the number of backgrounds, the color value and content of the background color, you can change it in values.


Source code: http://download.csdn.net/detail/forwardyzk/7993463

Use a shape. xml file and code to set the background color of different rounded corners

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.