Android itself defines the control and enables it to define its own properties in XML

Source: Internet
Author: User

Why do you define the view yourself?

The advantages of defining a view in Android development are obvious. For example, the following top navigation, which is designed for every interface of today's application, but each time the content is different. We can't configure a set of the same view in every layout resource? assuming that the <include layou= "@layout/xxx"/> Tag is used, although the reuse of layout files is overcome, the initialization settings of the associated view are not reusable (centralized). Need to use View.findviewbyid (ID) to initialize them every time.

With the " reusability " in mind, let's complete a quest to define the view for ourselves.

The first step is to create your own layout file that defines the view

There is no difference between the layout file created here and the layout file created for activity or fragment on weekdays, such as the following XML creates a picture above, and the following is the text's own definition view layout:

<?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:o rientation= "vertical" >    <imageview        android:id= "@+id/img"        android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:layout_gravity= "center"        android:background= "@ Drawable/ic_launcher "/>    <textview        android:id=" @+id/txt "android:layout_width=" Wrap_content        "        android:layout_height=" wrap_content "        android:layout_gravity=" center "        android:text=" ASDFASDF "/ ></LinearLayout>

The second step is to create your own definition view that inherits from view or its subclasses.Here's a little trick, namely inheriting your own definition of the elements in the view layout will save you a lot of code writing work. For example, in the above layout file, the root element is linearlayout, then our own definition view inherits LinearLayout:
Package Org.xiaom.customview.view;import Org.xiaom.customview.r;public Class MyView extends LinearLayout {private view root = null;//above imgprivate ImageView Imgview = null;//img below textprivate TextView Txtview = null;public MyView (Context con Text, AttributeSet attrs) {Super (context, attrs); Initview (context);} Public MyView (Context context) {super (context); Initview (context);} private void Initview (context context) {Layoutinflater Inflater = (layoutinflater) context.getsystemservice ( Context.layout_inflater_service); root = Inflater.inflate (R.layout.view_my_view, this,true); ImgView = (ImageView) Root.findviewbyid (r.id.img); Txtview = (TextView) Root.findviewbyid (R.id.txt);}}
The third step is to configure and use your own definition of the view in XML

After completing the above-defined actions, you can then use our own definition view as you would with Android native components. It is important to note that when you define a view in XML, you must use the full class name (the full name of the class).

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    android:layout_width= "Match_ Parent "    android:layout_height=" match_parent "    android:orientation=" vertical "><!--class full name    -- <org.xiaom.customview.view.myview        android:layout_width= "match_parent"        android:layout_height= "Wrap_ Content "/>    <button        android:layout_width=" match_parent "        android:layout_height=" Wrap_content "        android:layout_weight= "1"        android:text= "Aasdfasdfasdfa"/></linearlayout>

Next is very easy, we can use the Activity.this.setContentView (Layouid) method directly.


classmate A: OK, in the XML to use their own definition of properties, well ... I: I am the mother of idiot, and then Jiangzi I want to call the police (play DotA classmate eyes measured seconds to understand ~ ~ ~)
Fourth step, configure Declare-styleable to declare the properties you defineWe create a new Attr.xml file under res/values/and the root element is <resource>. Join? For example the following branches:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable    name= "MyView" >        <ATTR name= "img" format= "reference"/>        <attr name= "text" format= "string" ></attr>    </ Declare-styleable></resources>
The <attr> format has multiple values, which are not mentioned here. Many other information on this aspect can be found in the this blog post.
Fifth, configure yourself to define the properties and read, applyTo read your own definition of a property, we put it in the construction method of the view: the changed Myview.java becomes:
Package Org.xiaom.customview.view;import Org.xiaom.customview.r;public Class MyView extends LinearLayout {private view root = null;//above imgprivate ImageView imgview = null;//img below textprivate TextView txtview = null;//above image resource Iddrawable im g;//text content string Text;public MyView (context context, AttributeSet Attrs) {Super (context, attrs); TypedArray ta = context.obtainstyledattributes (attrs,r.styleable.myview); img = ta.getdrawable (r.styleable.myview_ IMG); text = ta.getstring (r.styleable.myview_text); Initview (context);//Remember to recycle (); Ta.recycle ();} private void Initview (context context) {Layoutinflater Inflater = (layoutinflater) context.getsystemservice ( Context.layout_inflater_service); root = Inflater.inflate (R.layout.view_my_view, this, true); Imgview = (ImageView) Root.findviewbyid (r.id.img); Txtview = (TextView) Root.findviewbyid (r.id.txt);// Pass the value of the defined property to the corresponding Viewimgview.setbackgroundresource (r.drawable.icon_consultation); Txtview.settext (text);}}
The following XML shows how to configure your own defined attributes in XML: Note: Carefully view the single gaze content in the following XML
<!--My ADT version number is v22.6.2-1085508, which supports its own initiative to find and verify declare-styleable; If your ADT does not support this feature, replace it with--><!--xmlns:myview= "http ://schemas.android.com/apk/org.xiaom.customview.myview ", here I did not verify--><linearlayout xmlns:myView="/http Schemas.android.com/apk/res-auto "    xmlns:android=" Http://schemas.android.com/apk/res/android "    android: Layout_width= "Match_parent"    android:layout_height= "match_parent"    android:orientation= "vertical" >    <!--full name--    <org.xiaom.customview.view.myview        android:layout_width= "Match_parent"        android:layout_height= "Wrap_content"        myview:img= "@drawable/icon_consultation"        myview:text= "self-defined text"/ >    <button        android:layout_width= "match_parent"        android:layout_height= "Wrap_content        " android:layout_weight= "1"        android:text= "Aasdfasdfasdfa"/></linearlayout>
All right, it's done. This is this blog post. The Eclipseproject of the instance. no matter what the need for the download of points, are bullying.


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.