Android custom control and enable it to customize properties in XML

Source: Internet
Author: User

Why do I want to customize view

The benefits of customizing view in Android development are obvious. For example, in the top navigation below, it is designed to appear on every interface of the app, but each time the content is different. We can't configure a set of identical view in each layout resource? If you use the <include layou= "@layout/xxx"/> Label, although the reuse of layout files is resolved, the initialization settings for the associated view are not reusable (centralized). You need to use View.findviewbyid (ID) every time to initialize them.

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

The first step is to create a layout file for the custom view

There is no difference between the layout file created here and the layout file created for activity or fragment on weekdays, for example, the following XML creates a picture above, and the following is a custom view layout for the text:

<?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 a custom view that inherits from the view or its subclasses.Here's a little trick, namely inheriting the elements in the custom 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 custom 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 the custom view in XML

After completing the customization above, we can then use our custom view as you would with Android native components. It is important to note that when custom view is declared in XML, the full class name (class full name--) must be used.

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

The next step is simple, we can use the Activity.this.setContentView (Layouid) method directly.


classmate A: Well, the use of custom attributes in XML, well ... I: My mother is idiot, and then Jiangzi I want to call the police (playing DotA students visual seconds to understand ~ ~ ~)
Fourth step, configure Declare-styleable to declare custom propertiesWe create a new Attr.xml file under res/values/and the root element is <resource>. Add 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. For more information on this, refer to this blog post.
Fifth step, configure custom properties and read, applyTo read a custom attribute we put it in the construction method of the view: the modified 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 custom property to the corresponding Viewimgview.setbackgroundresource (r.drawable.icon_consultation); Txtview.settext (text);}}
The following XML shows how to configure custom attributes in XML: Note: Carefully review the single-comment content in the following XML
<!--my ADT version is v22.6.2-1085508, which supports automatic search and verification of 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= "Custom 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. instance of Eclipse engineering . any download that requires points is 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.