PopWindow:基本使用與自定義PopWindow
PopWindow是Android開發中常見的控制元件,至於PopWindow是什麼道長就不在這裡安利了,這裡道長說一下PopWindoiw的常見的使用方式,不過道長還是沒有動圖。
一、基本使用
- 建立一個PopWindow物件
// 建立一個PopupWindow物件
// 設定PopupWindow要顯示的內容
ListView contentView = createListView();
// 設定PopupWindow的寬
int width = et_number.getWidth();
// 設定PopupWindow的高
int height = 500;
// 設定PopupWindow是否可以獲取焦點
boolean focusable = true;
popupWindow = new PopupWindow(contentView, width, height, focusable);
這裡PopWindow要顯示的物件程式碼如下:
private ListView createListView() {
ListView listView = (ListView) View.inflate(this, R.layout.layout_number_list, null);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long arg3) {
String number = (String) parent.getItemAtPosition(position);
et_number.setText(number);
popupWindow.dismiss();
}
});
adapter = new NumberListAdapter(HomeActivity.this,list);
listView.setAdapter(adapter);
return listView;
}
ListView的介面卡NumberListAdaper的實現程式碼如下:
public class NumberListAdapter extends BaseAdapter {
private ArrayList<String> mLists;
private Context mContext;
public NumberListAdapter(Context context, ArrayList<String> list) {
this.mContext = context;
this.mLists = list;
}
@Override
public int getCount() {
return mLists.size();
}
@Override
public Object getItem(int position) {
return mLists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(parent.getContext(), R.layout.item_number, null);
holder = new ViewHolder();
holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
holder.ib_del = (ImageView) convertView.findViewById(R.id.ib_del);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String number = mLists.get(position);
holder.tv_number.setText(number);
holder.ib_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mLists.remove(position);
notifyDataSetChanged(); // 通知ListView重新整理介面
}
});
return convertView;
}
static class ViewHolder {
TextView tv_number;
ImageView ib_del;
}
}
- 設定點選PopWindow之外彈窗消失
// 下面的兩行程式碼是用於實現在點PopupWindow之外的地方的時候可以隱藏PopupWindow
// 設定PopupWindow的外部可以點選
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable());
- 設定PopWindow的顯示位置
// 顯示PopupWindow
// 指定PopupWindow顯示在哪個View的下方
View anchor = et_number;
// 指定PopupWindow的x方向的偏移量 xoff > 0向右偏移
int xoff = 15;
// 指定PopupWindow的y方向的偏移量 以et_number的底部為yoff = 0,yoff > 0向下偏移
int yoff = -5;
popupWindow.showAsDropDown(anchor, xoff, yoff);
雖然指定的PopWindow是顯示在那個View的下方,但是可以通過PopWindow的偏移量來改變PopWindow的顯示位置。
二、自定義PopWindow
- 初始化構造方法
public MenuPop(Activity context, OnClickListener onClick) {
super(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.menu_popup, null);
h = context.getWindowManager().getDefaultDisplay().getHeight();
w = context.getWindowManager().getDefaultDisplay().getWidth();
click = onClick;
initView();
initPopWindow();
}
- 初始介面
private void initView(){
tv_regulation = (TextView) view.findViewById(R.id.tv_regulation);
tv_task = (TextView) view.findViewById(R.id.tv_task);
tv_card = (TextView) view.findViewById(R.id.tv_card);
tv_share = (TextView) view.findViewById(R.id.tv_share);
tv_regulation.setBackgroundColor(Color.parseColor(tv_bgColor));
tv_task.setBackgroundColor(Color.parseColor(tv_bgColor));
tv_card.setBackgroundColor(Color.parseColor(tv_bgColor));
tv_share.setBackgroundColor(Color.parseColor(tv_bgColor));
tv_regulation.setOnClickListener(click);
tv_task.setOnClickListener(click);
tv_card.setOnClickListener(click);
tv_share.setOnClickListener(click);
}
- 初始化PopWindow
private void initPopWindow(){
this.setOutsideTouchable(false);
this.setContentView(view);
// 設定彈出窗體的寬
this.setWidth(w);
// 設定彈出窗體的高
this.setHeight(LayoutParams.WRAP_CONTENT);
// 設定彈出窗體可點選
this.setFocusable(true);
// 設定彈出窗體動畫效果
// this.setAnimationStyle(R.style.AnimTop_gradualChange);
// 例項化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0x00FFFFFF);
//設定彈出窗體的背景
this.setBackgroundDrawable(dw);
// mMenuView新增OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷燬彈出框
// view.setBackgroundDrawable(new ColorDrawable());
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view.findViewById(R.id.rl_competeMenu).getBottom();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y > height) {
dismiss();
}
}
return true;
}
});
}
這裡道長又使用了另外一種方法來實現點選彈窗外,銷燬彈窗。當然也可已設定彈窗的動畫。
這兩種方式實現的PopWindow就到這裡,希望這篇部落格可以給你提供一些幫助。
原始碼下載
相關文章
- PopupWindow使用詳解(二)Popwindow製作常見花哨效果
- 餅圖元件的基本使用和自定義調整元件
- matlab自定義函式建立與使用Matlab函式
- samba 基本配置及自定義控制Samba
- GitLab使用自定義埠Gitlab
- ABP AutoMapper與自定義MappingAPP
- MySQL使用之五_自定義函式和自定義過程MySql函式
- 使用PHP實現詞法分析與自定義語言PHP詞法分析
- 使用 FVWM 自定義 Linux 桌面Linux
- jquery自定義事件的使用jQuery事件
- Facade 門面自定義使用
- 使用 Dockerfile 自定義 Nginx 映象DockerNginx
- 聊聊自定義SPI如何使用自定義標籤注入到spring容器中Spring
- 小程式自定義元件的使用元件
- draw.io 使用自定義字型自定義字型
- Angular 自定義管道 pipes 的使用Angular
- 在Flutter中使用自定義IconFlutter
- KingbaseES 自定義運算子使用示例
- springBoot自定義註解的使用Spring Boot
- Service或自定義物件使用LiveData物件LiveData
- 使用 voyager 如何自定義,使用者表
- spark:自定義分割槽,自定義排序,spark與jdbc,廣播變數等Spark排序JDBC變數
- pyhanlp 停用詞與使用者自定義詞典功能詳解HanLP
- SpringBoot + Spring Security 學習筆記(一)自定義基本使用及個性化登入配置Spring Boot筆記
- tymon/jwt-auth 的簡單使用與淺度刨析(使用自定義Model)JWT
- 自定義雙向迴圈連結串列基本函式介面函式
- Flutter自定義Widget和使用方法Flutter
- 如何使用小程式自定義元件功能元件
- Android自定義View之Canvas的使用AndroidViewCanvas
- Avalonia的自定義使用者元件元件
- 使用 Flutter 自定義一個 ProgressBar - IntervalProgressBarFlutter
- Vue 自定義元件使用 v-modelVue元件
- Angular rxjs裡自定義operator的使用AngularJS
- Vue 自定義元件directive使用總結Vue元件
- keycloak~使用自定義的註冊頁
- 使用stub快速生成自定義檔案
- 使用 CSS 自定義屬性(變數)CSS變數
- springboot如何使用自定義配置檔案Spring Boot
- 自定義梯形view與XRecyclerView的結合View