Code for dynamically adding a Panel framework to Android

Source: Internet
Author: User

This is a framework, but it is not that complicated. It is just a base class that is easy to expand. But at least it is framework code. Copy codeThe Code is as follows: package arui;

Import android. app. Activity;
Import android. OS. Handler;
Import android. OS. logoff;
Import android. OS. Message;
Import android. view. Gravity;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup. LayoutParams;
Import android. view. ViewManager;
Import android. widget. FrameLayout;

/**
* Base class for panel.
*
*/
Public abstract class BasePanel {

/**
* Left up position
*/
Public static final int LEFT_UP = 1;

/**
* Right up position
*/
Public static final int RIGHT_UP = 2;

/**
* Left bottom position
*/
Public static final int LEFT_BOTTOM = 3;

/**
* Right bottom position
*/
Public static final int RIGHT_BOTTOM = 4;

Private static final int DEFAULT_MARGIN = 10;

Private static final int SHOW_PANEL = 0;

Private Activity;

Private LayoutParams parameters;

Private View view = null;

Private int layoutId;

/**
* Constructor.
*
* @ Param activity
* This panel will be attached to the activity
* @ Param layoutId
* The panel's layout id
*/
Public BasePanel (Activity activity, int layoutId ){
This. activity = activity;
This. layoutId = layoutId;
}

/**
* The developer can use this method to add the panel to the Activity.
*
* @ Param act
* Activity
* @ Param params
* LayoutParams
*/
Public void attach (LayoutParams params ){
Parameters = params;
MHandler. sendMessage (mHandler. obtainMessage (SHOW_PANEL ));

}

/**
* The developer can use this method to add the panel to the Activity.
*
* @ Param act
* Activity
* @ Param position
* Int. You can use BasePanel. LEFT_UP, BasePanel. RIGHT_UP,
* BasePanel. RIGHT_BOTTOM or BasePanel. LEFT_BOTTOM.
*/
Public void attach (int position ){
Attach (position, DEFAULT_MARGIN,
DEFAULT_MARGIN );
}

/**
* The developer can use this method to add the panel to the Activity.
*
* @ Param act
* Activity
* @ Param position
* Int. You can use BasePanel. LEFT_UP, BasePanel. RIGHT_UP,
* BasePanel. RIGHT_BOTTOM or BasePanel. LEFT_BOTTOM.
* @ Param leftMargin
* Int, left margin.
* @ Param topMargin
* Int, top margin.
* @ Param rightMargin
* Int, right margin.
* @ Param bottomMargin
* Int, bottom margin.
*
*/
Public void attach (int position, int leftMargin, int topMargin,
Int rightMargin, int bottomMargin ){
FrameLayout. LayoutParams params = null;
Params = new FrameLayout. LayoutParams (LayoutParams. WRAP_CONTENT,
LayoutParams. WRAP_CONTENT );
Params. setMargins (leftMargin, topMargin, rightMargin, bottomMargin );
Switch (position ){
Case LEFT_UP:
Params. gravity = Gravity. LEFT;
Break;
Case RIGHT_UP:
Params. gravity = Gravity. RIGHT;
Break;
Case LEFT_BOTTOM:
Params. gravity = Gravity. LEFT | Gravity. BOTTOM;
Break;
Case RIGHT_BOTTOM:
Params. gravity = Gravity. RIGHT | Gravity. BOTTOM;
Break;
Default:
Break;
}
Attach (params );
}

/**
* The developer can use this method to remove the panel from the Activity.
*
*/
Public void remove (){
If (view! = Null ){
ViewManager mViewManager = (ViewManager) view. getParent ();
If (mViewManager! = Null ){
MViewManager. removeView (view );
}
}
}

Private Handler mHandler = new Handler (Looper. getMainLooper ()){

@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case SHOW_PANEL:
If (view = null ){
LayoutInflater factory = LayoutInflater. from (activity );
View = factory. inflate (layoutId, null );
}
DealwithPanel (view );
Remove ();
Activity. addContentView (view, parameters );
Break;
}
}

};

/**
* Do something with this panel.
*
* @ Param view
* View of the panel
*/
Public abstract void dealwithPanel (View view );
}

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.