Android development Note (3): android development Note

Source: Internet
Author: User

Android development Note (3): android development Note
1, <merge/> label

<Merge/> the tag debuted. When LayoutInflater encounters this label, it skips it and adds the elements in <merge/> to the parent element of <merge/>. Confused? Let's replace FrameLayout with <merge/> and rewrite the previous XML layout:

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    <TextView
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        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, both TextView and ImageView are directly added to the FrameLayout on the previous layer. Although it looks the same visually, the View hierarchy is simpler:

Obviously, in this case, <merge/> is used because the parent element of the ContentView of the Activity is always FrameLayout. If your layout uses LinearLayout as its root tag (for example), you cannot use this technique. <Merge/> is useful in other scenarios. For example, it works perfectly with <include/> labels. You can also use <merge/> when creating a custom combination View. Let's take a look at an example of creating a new View using <merge/>-OkCancelBar, which contains two buttons and can set button labels. The following XML is used to display a custom View on an image:

<merge
    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:okCancelBar="http://schemas.android.com/apk/res/com.example.android.merge">
    <ImageView  
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:scaleType="center"
        android:src="@drawable/golden_gate" />
    <com.example.android.merge.OkCancelBar
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"
        android:paddingTop="8dip"
        android:gravity="center_horizontal"
        android:background="#AA000000"
        okCancelBar:okLabel="Save"
        okCancelBar:cancelLabel="Don't save" />
</merge>

Shows the new layout effect:

The OkCancelBar code is very 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 uses 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();
    }
}

The two buttons are defined in the following XML. As you can see, we use the <merge/> label to add two buttons directly to OkCancelBar. Each button is included in the same external XML layout file for easy maintenance. We simply rewrite their id:

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

We have created a flexible and easy-to-maintain custom View with an efficient View hierarchy:

<Merge/> labels are extremely useful. However, it also has the following two restrictions:

· <Merge/> can only be used as the root tag of XML Layout

· When an Inflate layout file starting with <merge/>, you must specify a parent ViewGroup and set attachToRoot to true (see inflate (int, android. view. viewGroup, Boolean) method ).

2, multi-level 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; impo Rt android. widget. textView; import android. widget. toggleButton;/*** menu control header, which encapsulates the drop-down animation, dynamically generate the number of header buttons ** @ author yueyueniao */public class ExpandTabView extends LinearLayout implements OnDismissListener {private static final String TAG = "ExpandTabView"; private ToggleButton selectedButton; private ArrayList <String> mTextArray = new ArrayList <String> (); private ArrayList <RelativeLayout> mViewArray = new Arr AyList <RelativeLayout> (); private ArrayList <ToggleButton> mToggleButton = new ArrayList <ToggleButton> (); private 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 the value displayed by tabitem Based on the selected position */public void setTitle (String valueText, int position) {if (position <mToggleButton. size () {mToggleButton. get (position ). setText (valueText) ;}} public void setTitle (String title) {}/ *** obtain the value displayed by tabitem Based on the selected position */public String getTitle (int position) {if (position <mToggleButton. size () & mToggleButton. get (position ). getText ()! = Null) {return mToggleButton. get (position ). getText (). toString ();} return "";}/*** sets the number and initial value 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 (t Button); 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 onCli Ck (View v) {onPressBack () ;}}); r. setBackgroundColor (mContext. getResources (). getColor (R. color. popup_main_background); tButton. setOnClickListener (new OnClickListener () {@ Overridepublic void onClick (View view) {// initPopupWindow (); ToggleButton tButton = (ToggleButton) view; if (selectedButton! = Null & selectedButton! = TButton) {selectedButton. 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), displayWidth, displayHeight); popupWindow. setAnimationStyle (R. style. popupWindowAnimation); popupWindow. setFocusable (false); popupWindow. setOutsideTouchable (true);} if (selectedButton. isChecked () {if (! PopupWindow. isShowing () {showPopup (selectPosition);} else {popupWindow. setOnDismissListener (this); popupWindow. 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 (popu PWindow. getContentView ()! = MViewArray. get (position) {popupWindow. setContentView (mViewArray. get (position);} popupWindow. showAsDropDown (this, 0, 0);}/*** if the menu is in the expanded State, remove the menu */public boolean onPressBack () {if (popupWindow! = Null & popupWindow. isShowing () {popupWindow. 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 (). getdefadisplay display (). getWidth (); displayHeight = (Activity) mContext ). getWindowManager (). getdefadisplay display (). getHeight (); setOrientation (LinearLayout. HORIZONTAL) ;}@ Overridepublic void onDismiss () {showPopup (selectPosition); popupWindow. setOnDismissListener (null);} private OnButtonClickListener mOnButtonClickListener;/*** set the click listening event of tabitem */public void setOnButtonClickListener (OnButtonClickListener l) {mOnButtonClickListener = l ;} /*** custom tabitem click callback interface */public interface OnButtonClickListener {public void onClick (int selectPosition );}}



Android development knowledge SDK Android43 (API 18)

Have you heard of jdk? Jdk is a java development kit, and sdk is an android Development Kit. APIs are considered interfaces. Application Programming Interface (Application Programming Interface) is a number of pre-defined functions to provide applications and developers with the ability to access a group of routines based on a software or hardware, without accessing the source code or understanding the details of the internal working mechanism .. I do not know whether the explanation is clear.

What is the difference between android 30 development and android mobile phone development? What code does it need to change?

In fact, I think such a question should not be put on Baidu to know the question. After all, Baidu knows that it is only a common-sense Q & A platform. For specific questions, especially professional questions, they should be asked in professional forums, for example, you can go to a developer forum such as csdn.
As far as I know, android3.0 is more suitable for tablets.
Therefore, the current 2.2 tablet has limitations.
 

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.