亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

在Android中動(dòng)態(tài)添加Panel框架的實(shí)現(xiàn)代碼

 更新時(shí)間:2013年05月09日 10:12:32   作者:  
項(xiàng)目經(jīng)常會(huì)有這種需求,就是想動(dòng)態(tài)的在某個(gè)界面上添加一個(gè)Panel。比如,有一個(gè)按鈕,點(diǎn)擊后會(huì)顯示下拉菜單式的界面。這種需求,就屬于動(dòng)態(tài)添加一個(gè)Panel。需求多了,就要研究是否可以抽象出通用的框架代碼,以方便開發(fā),所以就有了以下內(nèi)容
這里說(shuō)是框架,說(shuō)的大了點(diǎn),其實(shí)沒有那么復(fù)雜,只是一個(gè)容易擴(kuò)展的基類而已。不過至少算是框架類的代碼。
復(fù)制代碼 代碼如下:

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); 

相關(guān)文章

最新評(píng)論