Android Development Notes (3)

Source: Internet
Author: User

1,<merge/> Label

The <merge/> label shines. When Layoutinflater encounters this tag, it skips over it and adds the elements inside the <merge/> to the parent element of the <merge/> . Are you confused? Let 's replace the framelayout with <merge/>and rewrite the previous XML layout:

<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >
Android:scaletype= "Center"
android:src= "@drawable/golden_gate"/>
<textview
Android:layout_marginbottom= "20dip"
Android:layout_gravity= "Center_horizontal|bottom"
Android:padding= "12dip"
Android:background= "#AA000000"
Android:textcolor= "#ffffffff"
android:text= "Golden Gate"/>
</merge>

in the new code, TextView and ImageView are added directly to the framelayout in the previous layer . Although visually similar, the View hierarchy is simpler:

obviously, using <merge/> On this occasion is because the Contentview parent of the Activity is always framelayout. If your layout uses LinearLayout as its root tag (for example), then you cannot use this technique. <merge/> is also useful on other occasions. For example, it can be perfectly combined with the <include/> tag. You can also use <merge/> when creating a custom combination View. Let's look at an example of creating a new View using <merge/>-Okcancelbar, which contains two buttons and can set the button label. The following XML is used to display a custom View on a picture :

<merge
Xmlns:android= "Http://schemas.android.com/apk/res/android" xmlns:okcancelbar= "http://schemas.android.com/apk/ Res/com.example.android.merge ">
Android:scaletype= "Center"
android:src= "@drawable/golden_gate"/>
<com.example.android.merge.okcancelbar
android:layout_gravity= "Bottom"
android:paddingtop= "8dip"
Android:gravity= "Center_horizontal"
Android:background= "#AA000000"
Okcancelbar:oklabel= "Save"
Okcancelbar:cancellabel= "Don ' t save"/>
</merge>

The new layout effect looks like this:

The Okcancelbar code is simple because the two buttons are defined in an external XML file and imported through the Layoutinflate class. As shown in the following code snippet,R.layout.okcancelbar takes Okcancelbar as the parent element:

public class Okcancelbar extends LinearLayout {
Public Okcancelbar (context context, AttributeSet Attrs) {
Super (context, attrs);
SetOrientation (horizontal);
Setgravity (Gravity.center);
Setweightsum (1.0f);
Layoutinflater.from (context). Inflate (R.layout.okcancelbar, this, true);
TypedArray array = context.obtainstyledattributes (attrs, R.styleable.okcancelbar, 0, 0);
String Text = array.getstring (R.styleable.okcancelbar_oklabel);
if (text = = null) Text = "OK";
(Button) Findviewbyid (R.ID.OKCANCELBAR_OK)). SetText (text);
Text = array.getstring (R.styleable.okcancelbar_cancellabel);
if (text = = null) Text = "Cancel";
(Button) Findviewbyid (R.id.okcancelbar_cancel)). SetText (text);
Array.recycle ();
}
}

two buttons are defined as the following as shown in XML. As you can see, we use the <merge/> tag to add two buttons directly to Okcancelbar. Each button is included in the same XML layout file from the outside and is easy to maintain; we simply rewrite their IDs:

<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >
<include
layout= "@layout/okcancelbar_button"
<include
layout= "@layout/okcancelbar_button"
Android:id= "@+id/okcancelbar_cancel"/>
</merge>

we have created a flexible and easy to maintain custom View, it has an efficient view hierarchy:

<merge/> tags are extremely useful. However it has also the following two restrictions:

· <merge/> can only be used as the root tag of an XML layout

· When you inflate a layout file that begins with <merge/> , you must specify a parent viewgroup, and you must set the attachtoroot to True(see inflate (int, android.view.ViewGroup, Boolean) method).

2, Multilevel drop-down list core class:: http://download.csdn.net/detail/minimicall/7956483

Package Com.example.view;import Java.util.arraylist;import Com.example.expandtabview.r;import android.app.Activity ; Import Android.content.context;import Android.util.attributeset;import Android.view.layoutinflater;import Android.view.view;import Android.widget.linearlayout;import Android.widget.popupwindow;import Android.widget.popupwindow.ondismisslistener;import Android.widget.relativelayout;import Android.widget.TextView ; Import android.widget.togglebutton;/** * Menu control header, encapsulated the next draw, dynamically generated head button number * * @author Yueyueniao */public class Expandtabview E Xtends LinearLayout implements Ondismisslistener {private static final String TAG = "Expandtabview";p rivate ToggleButton S Electedbutton;private arraylist<string> Mtextarray = new arraylist<string> ();p rivate ArrayList< relativelayout> Mviewarray = new arraylist<relativelayout> ();p rivate arraylist<togglebutton> Mtogglebutton = new arraylist<togglebutton> ();p rivate Context mcontext;private final int SMALL = 0;private int displaywidth;private int displayheight;private popupwindow popupwindow;private int selectposition;public Expandtabview (Context context) {super (context); init (context);} Public Expandtabview (context context, AttributeSet Attrs) {Super (context, attrs); init (context);}  /** * Set TabItem displayed value according to selected position */public void Settitle (String valueText, int position) {if (Position < mtogglebutton.size ()) {Mtogglebutton.get (position). SetText (ValueText);}} public void Settitle (string title) {}/** * Gets the value TabItem displayed based on the selected location */public String getTitle (int position) {if (Position < m Togglebutton.size () && mtogglebutton.get (position). GetText ()! = null) {return mtogglebutton.get (position). GetText (). toString ();} Return "";} /** * Sets the number and initial values of TabItem */public void SetValue (arraylist<string> textarray, arraylist<view> viewarray) {if ( Mcontext = = null) {return;} Layoutinflater Inflater = (layoutinflater) mcontext.getsystemservice (context.layout_inflater_service); MTextArray = textarray;for (int i = 0; i< Viewarray.size (); i++) {final Relativelayout R = new Relativelayout (mcontext); int maxheight = (int) (Displayheight * 0.7); Relativelayout.layoutparams RL = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.FILL_PARENT, MaxHeight ); rl.leftmargin = 10;rl.rightmargin = 10;r.addview (Viewarray.get (i), RL); Mviewarray.add (R); R.settag (SMALL); ToggleButton TButton = (ToggleButton) inflater.inflate (R.layout.toggle_button, this, false); AddView (TButton); View line = new TextView (mcontext), Line.setbackgroundresource (R.drawable.choosebar_line), if (I < viewarray.size ()- 1) {Linearlayout.layoutparams LP = new Linearlayout.layoutparams (2, LinearLayout.LayoutParams.FILL_PARENT); AddView ( Line, LP);} Mtogglebutton.add (TButton); Tbutton.settag (i); Tbutton.settext (Mtextarray.get (i)); R.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {onpressback ();}}); R.setbackgroundcolor (Mcontext.getresources (). GetColor (R.color.popup_main_background)); Tbutton.setonclicklistener (NewOnclicklistener () {@Overridepublic void OnClick (view view) {//Initpopupwindow (); ToggleButton TButton = (ToggleButton) view;if (Selectedbutton! = NULL && Selectedbutton! = TButton) {Selectedbutto N.setchecked (false);} Selectedbutton = Tbutton;selectposition = (Integer) selectedbutton.gettag (); startanimation (); if ( Monbuttonclicklistener! = null && tbutton.ischecked ()) {Monbuttonclicklistener.onclick (selectposition);}});}} private void Startanimation () {if (Popupwindow = = null) {Popupwindow = new Popupwindow (Mviewarray.get (selectposition), di Splaywidth, Displayheight);p Opupwindow.setanimationstyle (r.style.popupwindowanimation);p opupwindow.setfocusable (false);p opupwindow.setoutsidetouchable (true);} if (selectedbutton.ischecked ()) {if (!popupwindow.isshowing ()) {showPopup (selectposition);} else { Popupwindow.setondismisslistener (This);p Opupwindow.dismiss (); Hideview ();}} else {if (popupwindow.isshowing ()) {Popupwindow.dismiss (); Hideview ();}}} private void ShowPopup (int position){View TView = Mviewarray.get (selectposition). Getchildat (0); if (TView instanceof viewbaseaction) {viewbaseaction F = ( viewbaseaction) Tview;f.show ();} if (Popupwindow.getcontentview ()! = Mviewarray.get (position)) {Popupwindow.setcontentview (Mviewarray.get (position) );} Popupwindow.showasdropdown (this, 0, 0);} /** * If the menu is expanded, let the menu retract to */public boolean onpressback () {if (Popupwindow! = null && popupwindow.isshowing ()) {Popup Window.dismiss (); Hideview (); if (Selectedbutton! = null) {selectedbutton.setchecked (false);} return true;} else {return false;}} private void Hideview () {View TView = Mviewarray.get (selectposition). Getchildat (0); if (TView instanceof viewbaseaction) {Viewbaseaction F = (viewbaseaction) tview;f.hide ();}} private void init (context context) {Mcontext = Context;displaywidth = (Activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). GetWidth ();d isplayheight = (Activity) mcontext). Getwindowmanager (). Getdefaultdisplay (). GetHeight (); setorientation (linearlayout.horizontal);}@Overridepublic void Ondismiss () {showPopup (selectposition);p opupwindow.setondismisslistener (null);} Private Onbuttonclicklistener monbuttonclicklistener;/** * Set TabItem click-Listen event */public void Setonbuttonclicklistener ( Onbuttonclicklistener l) {monbuttonclicklistener = l;} /** * Custom TabItem Click Callback Interface */public interface Onbuttonclicklistener {public void OnClick (int selectposition);}}


Android Development Notes (3)

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.