The Android development family uses XML custom controls

Source: Internet
Author: User
Tags class definition

In the process of Android development, sometimes faced with a number of activities within the same layout, we need to write multiple times the same code, and this approach to our project maintenance also brought great inconvenience. So is there a viable way to split the same layout in activity very clearly? Of course, this is the time for custom controls to shine.

There are a number of ways to implement custom controls in Android, but today this blog only describes how to create custom controls using XML. Take a look at the following scenario, regardless of which page has a caption, including a centered text message and a Back button on the left. At this point we can split the above title into a control and then expose a Text property and a button click event in the control.

First let's start by creating a foreground XML file for placing the layout, as shown in the following code:

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android: background= "@color/test"    android:gravity= "center_vertical" >    <textview android:id=        "@+id/ Tvback "        android:layout_width=" wrap_content "        android:layout_height=" wrap_content "        android:text=" < "        android:layout_alignparentleft=" true "/>    <textview        android:id=" @+id/tvtitle "        Android:layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:text= "Login"        Android:layout_centerhorizontal= "true"/></relativelayout>

As you can see in the layout above, we just place two TextView, one for return and the other for title.

You might be able to guess when you see the smart one here. We also need some way to load the layout file in the custom control, And then at the time of loading, associating your own defined properties and events to the TextView of the TextView click event and caption above the Text property? Yes, see the more important Background association code class definition:

public class Titlecontrol extends Relativelayout {}

We can find that Titlecontrol is inherited directly from Relativelayout, so that Titlecontrol belongs to a view control. The next thing to do is to define attributes and events. Now suppose we need to define a mytext attribute, so what should we do? Take a look at the following code:

public class Titlecontrol extends Relativelayout {private TextView tvback, Tvtitle;    Private String title;    Public Titlecontrol (Context context) {super (context);        } public Titlecontrol (context context, AttributeSet Attrs) {Super (context, attrs);        View view = view.inflate (context, r.layout.activity_title, this);        Tvtitle = (TextView) View.findviewbyid (r.id.tvtitle);        Tvback = (TextView) View.findviewbyid (r.id.tvback);                           Tvback.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {        }        });        TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.title);        title = A.getstring (R.styleable.title_mytext);        A.recycle ();    Tvtitle.settext (title, TextView.BufferType.SPANNABLE);    } public String GetTitle () {return this.title;    public void Settitle (String title) {this.title = title; }}

In the above code we load the foreground page through the view's inflate method, and then we find the control that the foreground needs to associate with the Findviewbyid method. But where do we put our own defined properties? A relatively easy way to maintain is to place attributes in an XML file. Let's take a look at the XML code for the attribute:

<?xml version= "1.0" encoding= "Utf-8"?><resources> <declare-styleable name=    "title" >        < attr name= "MyText" format= "string"/>    </declare-styleable></resources>

Once we have defined the attribute XML, we can associate our own defined MyText property with the Text property of TextView by this code below. This is equivalent to implementing a custom property.

TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.title); title = A.getstring (r.styleable.title_mytext ); A.recycle (); Tvtitle.settext (title, TextView.BufferType.SPANNABLE);

We're going to have a custom event here, so how do we define an event associated with a TextView click? The answer is implemented by means of a callback method, see the definition of the callback method:

Private Ititlecallback Ititlecallback;    Public interface Ititlecallback {        void onbackclicklinear ();    }    public void Settitleclicklinear (Ititlecallback ititlecallback) {        this.ititlecallback = ititlecallback;    }

Then execute the following code when you click on the TextView:

Tvback = (TextView) View.findviewbyid (r.id.tvback);        Tvback.setonclicklistener (New Onclicklistener () {            @Override public            void OnClick (View v) {                if ( Ititlecallback!=null) {                    ititlecallback.onbackclicklinear ();}}        );

All right, let's get here today! If there is no, welcome to shoot bricks.

The Android development family uses XML custom controls

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.