在Android中動態添加Panel架構的實現代碼

來源:互聯網
上載者:User

這裡說是架構,說的大了點,其實沒有那麼複雜,只是一個容易擴充的基類而已。不過至少算是架構類的代碼。 複製代碼 代碼如下: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;

private Activity 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, 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);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.