Android Custom View properties, namespaces, and tools tags

Source: Internet
Author: User

Yesterday saw someone in the knowledge asked these 3 trivial little knowledge point, today simply tidy up a bit, in fact, these knowledge points is not difficult, but many developers usually rarely notice these,

The result is the development of the time is often the IDE error, development efficiency is very low, or look at the open source code when many places do not understand.

Considering that more and more people are migrating to Android studio today, everything in the Android studio environment will prevail. Compared to the Eclipse development environment, the two are similar,

The occasional difference is mainly due to the Gradle scripts introduced by Android Studio.

First look at the Tools tab.

This place a lot of people do not understand xmlns:tools this line of code is why, as if deleted after the program also did not affect, in fact, this tools tag is mainly used for ADT plug-in.

Many of the attributes in him can be very convenient for us to develop, but it does not affect the APK package that we eventually generate. For example, when you write an interface, you will usually give

TextView write the value of text, and then delete it when the development is complete, this operation is very troublesome, but now you can.

If you add Tools:text you can see the effect in the UI preview, but the actual run time will not be effective. Very convenient, the same as in the past when we developed the ListView is tired is unable to preview the ListView item effect,

You have to run it every time to see it. But now you just have to use the Tools tab.

Then you do not need to run your program directly in the interface preview to see the effect of item

The official documents given here are Http://tools.android.com/tech-docs/tools-attributes

Interested students can go up and see for themselves, try these tags, the development speed will be significantly improved ~ ~

Also the difference between res and res-auto.

1 xmlns:android= "Http://schemas.android.com/apk/res/android" 2 3 xmlns:customview= "Http://schemas.android.com/apk/res-auto"

These 2 are actually the ones that let you refer to the system's own properties, which allows you to use a custom attribute in the Lib library.

But this place should be aware that in eclipse, if you want to use your custom properties, you can't use Res-auto.

You have to replace the package name that your custom view belongs to, and if you're using a custom property that's been made into Lib

Then you can only use Res-auto, and in Android-studio, whether you write your own custom view

Or the custom view in the referenced Lib can only use the Res-auto notation. The name of the previous package

In the Android-studio, it is abandoned and unusable.

Finally, let's look at the differences and similarities between Typedarray and Attrs and the application in Custom view.

First we customize several properties

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3     <declare-styleablename= "Attrname">4         <attrname= "Name"format= "string"></attr>5         <attrname= "Number"format= "integer"></attr>6     </declare-styleable>7 8 9 </Resources>

Then the layout file

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:customview= "Http://schemas.android.com/apk/res-auto"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical">    <Com.example.administrator.popupmenu.CustomViewAndroid:layout_width= "100DP"Android:layout_height= "100DP"android:padding= "@dimen/padding"Customview:name= "@string/hello_world"Customview:number= "123" /></LinearLayout>

Then look at the source code of the custom view

1  PackageCom.example.administrator.popupmenu;2 3 ImportAndroid.content.Context;4 ImportAndroid.content.res.TypedArray;5 ImportAndroid.util.AttributeSet;6 ImportAndroid.util.Log;7 ImportAndroid.view.View;8 9 /**Ten * Created by Administrator on 2015/8/18. One  */ A  Public classCustomViewextendsView { -  -     Private Static FinalString TAG = CustomView.class. Getsimplename (); the  -      PublicCustomView (Context context, AttributeSet attrs) { -         Super(context, attrs); -TypedArray TA =context.obtainstyledattributes (Attrs, r.styleable.attrname); +String name =ta.getstring (r.styleable.attrname_name); -         intNumber = Ta.getinteger (R.styleable.attrname_number,-1); +LOG.E (TAG, "name=" + name + "number=" +Number ); A  at         /** - * The flaw in attrs is that if there is a similar reference inside the value, the correct value will not be taken . - * Requires extra -          * -          */ -          for(inti = 0; I < Attrs.getattributecount (); i++) { inLOG.E (TAG, "attrs name=" + attrs.getattributename (i) + "Attrs value=" +Attrs.getattributevalue (i)); -             //Take out the value of the actual pixel to             if(Attrs.getattributename (i). Equals ("padding")) { +                 ; -LOG.E (TAG, "attrs name=" + attrs.getattributename (i) + "Attrs value=" + getresources (). Getdimension ( Attrs.getattributeresourcevalue (i,-1))); the  *             } $             //This place can be seen typedarray than attrs to use more ~ at the same time can understand the difference between thePanax Notoginseng             if(Attrs.getattributename (i). Equals ("name")) { -                 ; theLOG.E (TAG, "attrs name=" + attrs.getattributename (i) + "Attrs value=" + getresources (). GetString ( Attrs.getattributeresourcevalue (i,-1))); +  A             } the         } + ta.recycle (); -  $  $     } -}

Finally, look at our output.

Android Custom View properties, namespaces, and tools tags

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.