The benefits of using the tag tag wisely in the layout of Android

Source: Internet
Author: User

Sometimes the same button page switch, in the code need to do multiple clicks of the separate processing, these are actually not such operations, the use of tag tags in the XML layout can be very good to deal with these problems; simplified operation

The layout file is as follows:

1 <LinearLayout2         Android:id= "@+id/colors"3 Android:layout_width= "Match_parent"4 Android:layout_height= "48dip"5 Android:layout_alignparentbottom= "true"6 Android:layout_marginbottom= "8dip"7 Android:layout_marginleft= "4dip"8 Android:layout_marginright= "4dip"9 android:orientation= "Horizontal" >Ten  One         <ImageView A             Android:layout_width= "0dip" - Android:layout_height= "Match_parent" - Android:layout_margin= "4dip" the Android:layout_weight= "1" - Android:background= "#FF666666" - Android:onclick= "oncolorclicked" - Android:tag= "#FF666666" /> +  -         <ImageView +             Android:layout_width= "0dip" A Android:layout_height= "Match_parent" at Android:layout_margin= "4dip" - Android:layout_weight= "1" - Android:background= "#FF96AA39" - Android:onclick= "oncolorclicked" - Android:tag= "#FF96AA39" /> -  in         <ImageView -             Android:layout_width= "0dip" to Android:layout_height= "Match_parent" + Android:layout_margin= "4dip" - Android:layout_weight= "1" the Android:background= "#FFC74B46" * Android:onclick= "oncolorclicked" $ Android:tag= "#FFC74B46" />Panax Notoginseng  -         <ImageView the             Android:layout_width= "0dip" + Android:layout_height= "Match_parent" A Android:layout_margin= "4dip" the Android:layout_weight= "1" + Android:background= "#FFF4842D" - Android:onclick= "oncolorclicked" $ Android:tag= "#FFF4842D" /> $  -         <ImageView -             Android:layout_width= "0dip" the Android:layout_height= "Match_parent" - Android:layout_margin= "4dip"Wuyi Android:layout_weight= "1" the Android:background= "#FF3F9FE0" - Android:onclick= "oncolorclicked" Wu Android:tag= "#FF3F9FE0" /> -  About         <ImageView $             Android:layout_width= "0dip" - Android:layout_height= "Match_parent" - Android:layout_margin= "4dip" - Android:layout_weight= "1" A Android:background= "#FF5161BC" + Android:onclick= "oncolorclicked" the Android:tag= "#FF5161BC" /> -     </LinearLayout>

Layout of:

Click on a different color to switch, change the color of the corresponding control;

The code is implemented as follows:

1 Public void oncolorclicked (View v) {23        int color = Color.parsecolor (V.gettag (). ToString ()); 4         ChangeColor (color); 5 6     }

1 Private voidChangeColor (intNewcolor) {2 3 Tabs.setindicatorcolor (newcolor);4 5         //Change ActionBar Color Just if a ActionBar is available6         if(Build.VERSION.SDK_INT >=Build.version_codes. Honeycomb) {7 8drawable colordrawable =Newcolordrawable (newcolor);9drawable bottomdrawable =getresources (). getdrawable (R.drawable.actionbar_bottom);TenLayerdrawable ld =NewLayerdrawable (Newdrawable[] {colordrawable, bottomdrawable}); One  A             if(Oldbackground = =NULL) { -  -                 if(Build.VERSION.SDK_INT <Build.version_codes. JELLY_BEAN_MR1) { the Ld.setcallback (drawablecallback); -}Else { - Getactionbar (). setbackgrounddrawable (LD); -                 } +  -}Else { +  ATransitiondrawable td =NewTransitiondrawable (Newdrawable[] {oldbackground, ld}); at  -                 //workaround for broken Actionbarcontainer drawable handling on -                 //Pre-api builds -                 // https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4 -                 if(Build.VERSION.SDK_INT <Build.version_codes. JELLY_BEAN_MR1) { - Td.setcallback (drawablecallback); in}Else { - Getactionbar (). setbackgrounddrawable (TD); to                 } +  -Td.starttransition (200); the  *             } $ Panax NotoginsengOldbackground =ld; -  the             // http://stackoverflow.com/questions/11002691/ Actionbar-setbackgrounddrawable-nulling-background-from-thread-handler +Getactionbar (). setdisplayshowtitleenabled (false); AGetactionbar (). setdisplayshowtitleenabled (true); the  +}

CurrentColor = Newcolor;

}

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.