The use of Android custom layouts!

Source: Internet
Author: User

Inherit ViewGroup;

The left margin of the custom control, the right margin, the top margin, the bottom margin;


Java code;

Package com.example.customview1406_04myviwegroup;

Import Android.content.Context;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.view.ViewGroup;

/**
* A custom layout that specifies how each child control is displayed
*
* @author gsd1403
*
*/
public class Myviewgroup extends ViewGroup {

Public Myviewgroup (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
}

/**
* Measure the size of the control
*/
@Override
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
TODO auto-generated Method Stub
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Measure the size of a child control
Number of child controls
int childCount = This.getchildcount ();
for (int i = 0; i < ChildCount; i++) {
Get child controls
View view = This.getchildat (i);
View.measure (Widthmeasurespec, Heightmeasurespec);
}
}

public void Setshowview (int index) {
if (Index > This.getchildcount ()) {
Return
}
if (index = = 0) {
View view = This.getchildat (0);
View.setvisibility (view.visible);
This.getchildat (1). setvisibility (View.gone);
} else if (index = = 1) {
This.getchildat (1). setvisibility (view.visible);
This.getchildat (0). setvisibility (View.gone);
}
}

/**
* Specifies how child controls display similar to OnDraw
*/
@Override
protected void OnLayout (Boolean changed, int l, int t, int r, int b) {
Get the first control Text.xml
View view1=this.getchildat (0);
View1.layout (0, 0, view1.getmeasuredwidth (),
View1.getmeasuredheight ());
//
View View2=this.getchildat (1);
View2.layout (), View2.getmeasuredwidth (),
View2.getmeasuredheight ());
int childCount = This.getchildcount ();
int left = 0;
for (int i = 0; i < ChildCount; i++) {
View view = This.getchildat (i);
if (view.getvisibility ()! = View.gone) {
View.layout (left, 0, left + view.getmeasuredwidth (),
View.getmeasuredheight ());
left = left + view.getmeasuredwidth ();
}
}

}

}

Mainactivity Code;


Package com.example.customview1406_04myviwegroup;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends Activity {
Myviewgroup Myviewgroup;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
myviewgroup= (Myviewgroup) Findviewbyid (R.id.myviewgroup);
Button btnaudio= (button) Findviewbyid (R.id.button_audio);
Btnaudio.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Myviewgroup.setshowview (1);
}
});

Button btntext= (button) Findviewbyid (R.id.button_text);
Btntext.setonclicklistener (New Onclicklistener () {

@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Myviewgroup.setshowview (0);
}
});
}

@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}

}


layout file; code;

<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
Tools:context= ". Mainactivity ">

<com.example.customview1406_04myviwegroup. Myviewgroup
Android:id= "@+id/myviewgroup"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
>
<include layout= "@layout/text"/>
<include layout= "@layout/audio"/>
</com.example.customview1406_04myviwegroup. Myviewgroup>

</RelativeLayout>



Click the Audio button after the effect such as;




The use of Android custom layouts!

Related Article

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.