Android PopupWindow 使用小Demo奔脐,以及從底部彈起的半透明背景的PopupWindow通用父類

關(guān)于PopupWindow的使用俄周,網(wǎng)上各種介紹,不勝枚舉髓迎。我這里記錄一下自己寫的一個小demo(備忘)峦朗,背景是半透明的,從底部彈起的popupwindow框排龄,然后封裝成通用父類波势。效果如下:

I(img.png

下面是通用父類的代碼(BasePopupWindow.java):

protected Activity activity;
protected View mContentView;
protected OnItemClickListener listener;

public BasePopupWindow(Activity activity) {
    this.activity = activity;
    initView();
}

/**
 * 初始化控件
 */
private void initView() {
    LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mContentView = inflater.inflate(getLayout(), null);

    bindView();
    dealEvent();

    //設(shè)置PopupWindow的View
    this.setContentView(mContentView);
    //設(shè)置PopupWindow彈出窗體的寬
    this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //設(shè)置PopupWindow彈出窗體的高
    this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
    //設(shè)置PopupWindow彈出窗體可點擊
    this.setFocusable(true);
    //設(shè)置PopupWindow彈出窗體動畫效果
    this.setAnimationStyle(R.style.bottom_anim);
    //關(guān)閉事件
    this.setOnDismissListener(new popupDismissLister());

    backgroundAlpha(0.5f);
    //mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
    mContentView.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            int height = mContentView.findViewById(getPopupTopView()).getTop();
            int y = (int) event.getY();
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (y < height) {
                    dismiss();
                }
            }
            return true;
        }
    });
}

/**
 * 獲取布局文件
 *
 * @return
 */
protected abstract int getLayout();

/**
 * 獲取PopupWindow最高控件
 *
 * @return
 */
protected abstract int getPopupTopView();

/**
 * 綁定控件
 */
protected abstract void bindView();

/**
 * 處理相關(guān)事件
 */
protected abstract void dealEvent();

/**
 * 查找View
 *
 * @param id   控件的id
 * @param <VT> View類型
 * @return
 */
protected <VT extends View> VT getViewById(@IdRes int id) {
    return (VT) mContentView.findViewById(id);
}

/**
 * 設(shè)置popupwindow外面背景透明度
 *
 * @param bgalpha 透明度  0-1   0-透明   1-不透明
 */
private void backgroundAlpha(float bgalpha) {
    WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
    lp.alpha = bgalpha;
    activity.getWindow().setAttributes(lp);
}

/**
 * 添加子空間的監(jiān)聽事件
 *
 * @param listener
 */
public void addOnItemClickListener(OnItemClickListener listener) {
    this.listener = listener;
}

public interface OnItemClickListener {
    void onClicked(int flag);
}

private class popupDismissLister implements PopupWindow.OnDismissListener {
    @Override
    public void onDismiss() {

        backgroundAlpha(1f);
    }
}

使用時,只需要繼承這個通用類接口橄维,以下便是我的小例子尺铣。

RemindPopWindow.java

public final static int FLAG_EDIT = 0;
public final static int FLAG_DELETE = 1;

TextView mEdit, mDelete, mCancel;

public RemindPopWindow(Activity activity) {
    super(activity);
}

@Override
protected int getLayout() {
    return R.layout.window_remind;
}

@Override
protected int getPopupTopView() {
    return R.id.layout;
}

@Override
protected void bindView() {
    mEdit = getViewById(R.id.tv_edit);
    mDelete = getViewById(R.id.tv_delete);
    mCancel = getViewById(R.id.tv_cancel);

}

@Override
protected void dealEvent() {
    mEdit.setOnClickListener(this);
    mDelete.setOnClickListener(this);
    mCancel.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    if (view == mEdit) {
       if (listener!=null)listener.onClicked(FLAG_EDIT);
    }

    if (view == mDelete) {
        if (listener!=null)listener.onClicked(FLAG_DELETE);
    }
    if (view == mCancel) {
        dismiss();
    }
}

提示框的布局window_remind.xml,ViewGroup為LinearLayout 挣郭,id為layout迄埃。

<TextView
    android:id="@+id/tv_edit"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="編輯"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

<TextView
    android:id="@+id/tv_delete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="1px"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="刪除"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

<TextView
    android:id="@+id/tv_cancel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:background="@android:color/white"
    android:gravity="center"
    android:minHeight="60dp"
    android:text="取消"
    android:textColor="#5f5f5f"
    android:textSize="16sp" />

這樣一來,提示的對話框就寫好了兑障,使用時侄非,只要在對應(yīng)的布局中顯示PopupWindow就行了。

具體代碼流译,請見:https://github.com/Henry-Mark/PopWindowDemo逞怨。

初次寫,如有啥問題福澡,還請大神們指正叠赦,謝謝。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末革砸,一起剝皮案震驚了整個濱河市除秀,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌算利,老刑警劉巖册踩,帶你破解...
    沈念sama閱讀 216,544評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異效拭,居然都是意外死亡暂吉,警方通過查閱死者的電腦和手機胖秒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評論 3 392
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來慕的,“玉大人阎肝,你說我怎么就攤上這事“菇郑” “怎么了风题?”我有些...
    開封第一講書人閱讀 162,764評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長低散。 經(jīng)常有香客問我俯邓,道長骡楼,這世上最難降的妖魔是什么熔号? 我笑而不...
    開封第一講書人閱讀 58,193評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮鸟整,結(jié)果婚禮上引镊,老公的妹妹穿的比我還像新娘。我一直安慰自己篮条,他們只是感情好弟头,可當(dāng)我...
    茶點故事閱讀 67,216評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著涉茧,像睡著了一般赴恨。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上伴栓,一...
    開封第一講書人閱讀 51,182評論 1 299
  • 那天伦连,我揣著相機與錄音,去河邊找鬼钳垮。 笑死惑淳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的饺窿。 我是一名探鬼主播歧焦,決...
    沈念sama閱讀 40,063評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼肚医!你這毒婦竟也來了绢馍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評論 0 274
  • 序言:老撾萬榮一對情侶失蹤肠套,失蹤者是張志新(化名)和其女友劉穎舰涌,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體糠排,經(jīng)...
    沈念sama閱讀 45,329評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡舵稠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,543評論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片哺徊。...
    茶點故事閱讀 39,722評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡室琢,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出落追,到底是詐尸還是另有隱情盈滴,我是刑警寧澤,帶...
    沈念sama閱讀 35,425評論 5 343
  • 正文 年R本政府宣布轿钠,位于F島的核電站巢钓,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏疗垛。R本人自食惡果不足惜症汹,卻給世界環(huán)境...
    茶點故事閱讀 41,019評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望贷腕。 院中可真熱鬧背镇,春花似錦、人聲如沸泽裳。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽涮总。三九已至胸囱,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瀑梗,已是汗流浹背烹笔。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留夺克,地道東北人箕宙。 一個月前我還...
    沈念sama閱讀 47,729評論 2 368
  • 正文 我出身青樓,卻偏偏與公主長得像铺纽,于是被迫代替她去往敵國和親柬帕。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,614評論 2 353

推薦閱讀更多精彩內(nèi)容