Multiple Android interface reuse a layout file

Source: Internet
Author: User

1.layout_common.xml

reusing layout files

<?XML version= "1.0" encoding= "Utf-8"?><!--Reusing layout files -<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" >    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1" />    <ButtonAndroid:id= "@+id/common_button1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Button 1" />    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1" />    <ButtonAndroid:id= "@+id/common_button2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button 2" />    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1" />    <ButtonAndroid:id= "@+id/common_button3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button 3" />    <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_weight= "1" /></LinearLayout>

2.layout_main.xml

Main layout file, where the reuse layout file is referenced

<?XML version= "1.0" encoding= "Utf-8"?><!--Main layout file -<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical" >    <RelativelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "0DP"Android:layout_margin= "16DP"Android:layout_weight= "1" >    </Relativelayout>    <!--referencing a reusable layout file in a layout file -    <includeLayout= "@layout/layout_common" /></LinearLayout>

3.commonview.java

Re-use the layout file instantiation. Individually encapsulated, interface callback. Avoid repeating the layout file, avoid repeating the control, avoid repeating the setting of the Listener method

 PackageCom.example.mytestapp;Importandroid.app.Activity;ImportAndroid.content.Context;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;/*** Reusing layout file instantiation*/ Public classCommonviewImplementsOnclicklistener {//Interface     Public InterfaceOncommonviewclick { Public voidOnbutton1click (View v);  Public voidOnbutton2click (View v);  Public voidOnbutton3click (View v); }     Public voidSetlistener (Oncommonviewclick listener) { This. Listener =Listener;    } Context Mcontext;    Oncommonviewclick Listener;  PublicCommonview (Context context) { This. Mcontext =context; }     PublicButton button1, Button2, Button3;  PublicCommonview init () {button1=( Button) ((Activity) mcontext). Findviewbyid (R.id.common_button1); Button2=( Button) ((Activity) mcontext). Findviewbyid (R.id.common_button2); Button3=( Button) ((Activity) mcontext). Findviewbyid (R.id.common_button3); Button1.setonclicklistener ( This); Button2.setonclicklistener ( This); Button3.setonclicklistener ( This); return  This; } @Override Public voidOnClick (View v) {if(Listener = =NULL)            return; Switch(V.getid ()) { CaseR.id.common_button1:listener.onbutton1click (v);  Break;  CaseR.id.common_button2:listener.onbutton2click (v);  Break;  CaseR.id.common_button3:listener.onbutton3click (v);  Break; default:             Break; }    }}

4.mainactivity.java

Instantiation of the main interface

 PackageCom.example.mytestapp;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Toast;/** Question: * 1. How do I change the text or color of a control with code? */ Public classMainactivityextendsActivityImplementsCommonview.oncommonviewclick {intClicktimes = 0; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.layout_main); //instantiate the layout file here and implement the Listener interface method//the instantiation of a reusable block of code can be done directly with one line of code        NewCommonview ( This). Init (). Setlistener ( This); } @Override Public voidOnbutton1click (View v) {clicktimes++; //here's how to implement the corresponding Click eventToast.maketext ( This, "You clicked button" + Clicktimes + "Times", Toast.length_short). Show (); } @Override Public voidOnbutton2click (View v) {//here's how to implement the corresponding Click event} @Override Public voidOnbutton3click (View v) {//here's how to implement the corresponding Click event    }}

Multiple Android interface reuse a layout file

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.