Implementing code for dynamically adding a panel frame to Android _android

Source: Internet
Author: User
Here is the framework, said the point, in fact, not so complex, but an easy to expand the base class just. But at least it's a frame class code.
Copy Code code as follows:

Package Arui;

Import android.app.Activity;
Import Android.os.Handler;
Import Android.os.Looper;
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;

activity of private activity;

private Layoutparams parameters;

Private view view = NULL;

private int layoutid;

/**
* constructor.
*
* @param activity
* This panel would 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 the "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 the "to" add the panel to the activity.
*
* @param Act
* Activity
* @param position
* Int. 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, Default_margin,
Default_margin);
}

/**
* The developer can use the "to" add the panel to the activity.
*
* @param Act
* Activity
* @param position
* Int. 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 (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 and this panel.
*
* @param view
* View of the Panel
*/
public abstract void Dealwithpanel (view view);
}

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.