How to get non-custom attributes in the XML interface in Android

Source: Internet
Author: User

Get custom properties Everyone is very familiar with it, not much to say (define Declare-styleable,context.obtainstyledattributes Balabala ... )

Let's talk about how to get non-custom attributes, such as android:entries,android:gravity and so on, the way the above is not working, because you can't get Android. R.styleable inside, here are some of the ways I tried it out:

1. Traverse the AttributeSet, basically can put all the attributes in the XML out, but a little, the value to be able to use, need to undergo a certain processing, the specific code is as follows:

1         //Find Entries2         intCount =Attrs.getattributecount ();3          for(inti = 0; I < count; i++) {4             if("Entries". Equals (Attrs.getattributename (i))) {5                 intResid = Attrs.getattributeresourcevalue (i, 0);6                 if(Resid > 0) {7string[] Entries =context.getresources (). Getstringarray (RESID);8                     if(Entries! =NULL) {9                         //Get GravityTen                         intg =-1; One                          for(intj = 0; J < Count; J + +) { A                             if("Gravity". Equals (Attrs.getattributename (j))) { -g =J; -                                  Break; the                             } -                         } -                         Final intGravity =Attrs.getattributeintvalue (g, gravity.center); -                         //Create Adapter +arrayadapter<charsequence> adapter =NewArrayadapter<charsequence>(context, R.layout.item_spinner_top, R.id.text, entries) { - @Override +                              PublicView GetView (intposition, View Convertview, ViewGroup parent) { AView view =Super. GetView (position, convertview, parent); atTextView Text =Butterknife.findbyid (view, r.id.text); - Text.settextcolor (textcolor); - text.settextsize (DIMENUTILS.PX2DP (textSize)); - text.setgravity (gravity); -                                 returnview; -                             } in  - @Override to                              PublicView Getdropdownview (intposition, View Convertview, ViewGroup parent) { +View Dropdownview =Super. Getdropdownview (position, convertview, parent); -TextView Text =Butterknife.findbyid (Dropdownview, r.id.text); the Text.settextcolor (dropdowntextcolor); * text.settextsize (DIMENUTILS.PX2DP (textSize)); $ text.setgravity (gravity);Panax Notoginseng                                 returnDropdownview; -                             } the                         }; + Adapter.setdropdownviewresource (r.layout.item_spinner_dropdown); A Setadapter (adapter); the                     } +                 } -                  Break; $             } $}

2. Or use Context.obtainstyledattributes, the array can be taken from the android.r.attr inside (do not need to styleable), to deal with the same as before, very useful, recommended

    Private Static Final int[] Rx_spinner_override_attrs ={Android. R.attr.entries, Android.        R.attr.gravity}; A=context.obtainstyledattributes (attrs,rx_spinner_override_attrs); if(A.hasvalue (0) ) {charsequence[] entries= A.gettextarray (0); Final intGravity = A.getint (1, Gravity.center); //Create Adapterarrayadapter<charsequence> adapter =NewArrayadapter<charsequence>(context, R.layout.item_spinner_top, R.id.text, entries) {@Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {View View=Super. GetView (position, convertview, parent); TextView text=Butterknife.findbyid (view, R.id.text);                    Text.settextcolor (TextColor);                    Text.settextsize (DIMENUTILS.PX2DP (textSize));                    Text.setgravity (Gravity); returnview; } @Override PublicView Getdropdownview (intposition, View Convertview, ViewGroup parent) {View Dropdownview=Super. Getdropdownview (position, convertview, parent); TextView text=Butterknife.findbyid (Dropdownview, R.id.text);                    Text.settextcolor (Dropdowntextcolor);                    Text.settextsize (DIMENUTILS.PX2DP (textSize));                    Text.setgravity (Gravity); returnDropdownview;            }            };            Adapter.setdropdownviewresource (R.layout.item_spinner_dropdown);        Setadapter (adapter); } a.recycle ();

In the use of inadvertently found another problem, tint related properties even set, under 5.0 is not available, the reason is that this is the API level 21 is introduced, below this machine is ignored, then to be compatible with the high and low version, you can use the Tint property in the V7 package, Android.support.v7.appcompact.r.attr.backgroundtint, such as the use of the time do not use Android:backgroundtint, app:backgroundtint can be used instead.

How to get non-custom attributes in the XML interface in Android

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.