Android自定義PopupWindow小案例
PopupWindow是我們開發(fā)中的??椭?,使用起來(lái)也比較簡(jiǎn)單方便。
寫了個(gè)最簡(jiǎn)單地自定義PopupWindow,記錄下來(lái),方便以后使用時(shí)直接在上面改動(dòng)就可以。
/**
* @param
* @author ldm
* @description 自定義PopupWindow
* @time 2016/9/29 15:26
*/
public class CustomPopup extends PopupWindow {
//上下文
private Context mContext;
// PopupWindow中控件點(diǎn)擊事件回調(diào)接口
private IPopuWindowListener mOnClickListener;
//PopupWindow布局文件中的Button
private Button alarm_pop_btn;
/**
* @description 構(gòu)造方法
* @author ldm
* @time 2016/9/30 9:14
* @param
*/
public CustomPopup(Context mContext, int width, int height, IPopuWindowListener listener) {
super(mContext);
this.mContext = mContext;
this.mOnClickListener = listener;
//獲取布局文件
View mContentView = LayoutInflater.from(mContext).inflate(R.layout.alarm_disopse_pop, null);
//設(shè)置布局
setContentView(mContentView);
// 設(shè)置彈窗的寬度和高度
setWidth(width);
setHeight(height);
//設(shè)置能否獲取到焦點(diǎn)
setFocusable(false);
//設(shè)置PopupWindow進(jìn)入和退出時(shí)的動(dòng)畫效果
setAnimationStyle(R.style.popwindow_exit_anim_style);
setTouchable(true); // 默認(rèn)是true,設(shè)置為false,所有touch事件無(wú)響應(yīng),而被PopupWindow覆蓋的Activity部分會(huì)響應(yīng)點(diǎn)擊
// 設(shè)置彈窗外可點(diǎn)擊,此時(shí)點(diǎn)擊PopupWindow外的范圍,Popupwindow不會(huì)消失
setOutsideTouchable(false);
//外部是否可以點(diǎn)擊,設(shè)置Drawable原因可以參考:http://blog.csdn.net/harvic880925/article/details/49278705
setBackgroundDrawable(new BitmapDrawable());
// 設(shè)置彈窗的布局界面
initUI();
}
/**
* 初始化彈窗列表
*/
private void initUI() {
//獲取到按鈕
alarm_pop_btn = (Button) getContentView().findViewById(R.id.alarm_pop_btn);
//設(shè)置按鈕點(diǎn)擊事件
alarm_pop_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (null != mOnClickListener) {
mOnClickListener.dispose();
}
}
});
}
/**
* 顯示彈窗列表界面
*/
public void show(View view) {
int[] location = new int[2];
view.getLocationOnScreen(location);
//Gravity.BOTTOM設(shè)置在view下方,還可以根據(jù)location來(lái)設(shè)置PopupWindowj顯示的位置
showAtLocation(view, Gravity.BOTTOM, 0, 0);
}
/**
* @param
* @author ldm
* @description 點(diǎn)擊事件回調(diào)處理接口
* @time 2016/7/29 15:30
*/
public interface IPopuWindowListener {
void dispose();
}
}
注:設(shè)置PopupWindow的寬高還可以通過LayoutParams來(lái)設(shè)置,比如:
//通過LayoutParams來(lái)設(shè)置PopupWindow的高度和寬度
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams)mContentView.getLayoutParams();
lp.widht=400;
lp.height = 180;
mContentView.setLayoutParams(lp);
//但是直接這樣寫獲取到的lp可能為空,我們?cè)讷@取一個(gè)View的LayoutParams時(shí),通常應(yīng)該這樣寫:
//在addOnGlobalLayoutListener監(jiān)聽中來(lái)獲取View的LayoutParams mContentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
ViewGroup.LayoutParams lp = (ViewGroup.LayoutParams) mContentView.getLayoutParams();
lp.height = 180;
lp.width = 400;
mContentView.setLayoutParams(lp);
}
});
在Activity中使用:
private CustomPopup alarmPopup;
....
//初始化PopupWindow這里通過數(shù)據(jù)200來(lái)設(shè)置PopupWindow高度
alarmPopup=new CustomPopup(getActivity(), ViewGroup.LayoutParams.MATCH_PARENT, 200, this);//這里的this是指當(dāng)前Activity實(shí)現(xiàn)了PopupWindow中IPopuWindowListener接口
//彈出PopupWindow
@Override
protected void widgetClick(View v) {
super.widgetClick(v);
switch (v.getId()) {
case R.id.popup:
alarmPopup.show(v);
break;
}
}
//處理PopupWindow中按鈕點(diǎn)擊回調(diào)
@Override
public void dispose() {
//TODO sth
if (alarmPopup.isShowing()) {
alarmPopup.dismiss();//關(guān)閉PopupWindow
}
}
PopupWindow對(duì)應(yīng) 的布局界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pop_ll"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#404040"
android:orientation="horizontal">
<Button
android:id="@+id/alarm_pop_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="center"
android:text="@string/dispose"
android:textColor="#FFFFFF"
android:textSize="18sp" />
</LinearLayout>
動(dòng)畫style:
<style name="popwindow_exit_anim_style" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定顯示的動(dòng)畫xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的動(dòng)畫xml -->
</style>
popshow_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="100%p" android:toYDelta="0" /> <alpha android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0" /> </set>
pophidden_anim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="500" android:fromYDelta="0" android:toYDelta="50%p" /> <alpha android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Dialog風(fēng)格彈出框的Activity
- Android實(shí)現(xiàn)可使用自定義透明Dialog樣式的Activity完整實(shí)例
- Android解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件的方法
- Android自定義仿微信PopupWindow效果
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義PopupWindow簡(jiǎn)單小例子
- Android開發(fā):微信授權(quán)登錄與微信分享完全解析
- Android 解決dialog彈出時(shí)無(wú)法捕捉Activity的back事件問題
相關(guān)文章
Android入門之在Activity之間穿梭的Intent
Intent可以用來(lái)啟動(dòng)Activity(startActivity(Intent))、Serveice(startService(Intent))等組件,可以用來(lái)綁定Activity和Service以建立它們之間的通信(bindServiceConnaction(Intent,ServiceConnection,int)),可以作為Broadcast Intent發(fā)送給廣播接收器2021-10-10
android 下載時(shí)文件名是中文和空格會(huì)報(bào)錯(cuò)解決方案
項(xiàng)目中遇到了下載文件文件名是中文而且還有空格如果不對(duì)連接進(jìn)行處理下載就會(huì)報(bào)錯(cuò)要想解決這個(gè)問題只需對(duì)你的url進(jìn)行編碼然后替換空格用編碼表示,感興趣的朋友可以詳細(xì)了解下2013-01-01
Android 自定義View結(jié)合自定義TabLayout實(shí)現(xiàn)頂部標(biāo)簽滑動(dòng)效果
小編最近在做app的項(xiàng)目,需要用到tablayout實(shí)現(xiàn)頂部的滑動(dòng)效果,文中代碼用到了自定義item,代碼也很簡(jiǎn)單,感興趣的朋友跟隨腳本之家小編一起看看吧2018-07-07
Android獲取手機(jī)號(hào)碼和運(yùn)營(yíng)商信息的方法
這篇文章主要介紹了Android獲取手機(jī)號(hào)碼和運(yùn)營(yíng)商信息的方法,以實(shí)例形式完整講述了獲取手機(jī)號(hào)碼和運(yùn)營(yíng)商信息的技巧,代碼中包含完整的注釋說(shuō)明,需要的朋友可以參考下2015-01-01
Android中SwipeBack實(shí)現(xiàn)右滑返回效果
這篇文章主要介紹了Android中SwipeBack實(shí)現(xiàn)右滑返回效果的相關(guān)資料,需要的朋友可以參考下2016-02-02
AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果
這篇文章主要為大家詳細(xì)介紹了AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android apk 插件啟動(dòng)內(nèi)存釋放問題
這篇文章主要介紹了Android apk 插件啟動(dòng)內(nèi)存釋放問題的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android SQLite數(shù)據(jù)庫(kù)的增 刪 查找操作
這篇文章主要介紹了Android SQLite數(shù)據(jù)庫(kù)的增 刪 查找操作,需要的朋友可以參考下2017-02-02
Flutter調(diào)用Android和iOS原生代碼的方法示例
這篇文章主要給大家介紹了關(guān)于Flutter調(diào)用Android和iOS原生代碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路
這篇文章主要介紹了Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路的相關(guān)資料,在開發(fā)網(wǎng)路狀態(tài)的時(shí)候需要先判斷是否開啟之后在提示用戶進(jìn)行開啟操作,需要的朋友可以參考下2017-08-08

