国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

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

2020-04-11 12:25:44
字體:
來源:轉載
供稿:網友
這里說是框架,說的大了點,其實沒有那么復雜,只是一個容易擴展的基類而已。不過至少算是框架類的代碼。
復制代碼 代碼如下:

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江山市| 淮阳县| 陕西省| 沁阳市| 武川县| 嘉兴市| 永丰县| 通化县| 台安县| 即墨市| 稷山县| 南溪县| 壶关县| 永兴县| 轮台县| 龙岩市| 墨脱县| 垣曲县| 新闻| 永修县| 嘉鱼县| 阿合奇县| 抚远县| 黔江区| 刚察县| 安塞县| 炉霍县| 梧州市| 南召县| 综艺| 论坛| 郑州市| 罗城| 宜黄县| 措勤县| 平陆县| 海晏县| 靖西县| 泰顺县| 太原市| 三门峡市|