根據(jù)網(wǎng)上的popupwind總結(jié),整理一份自己實(shí)踐過的知識,進(jìn)行備用
1 概述
自己理解的是他就是以彈出對話框,與AlertDialog的作用類似,popupwindow
更靈活,其位置可以根據(jù)你自己的需求隨意設(shè)置,而AlertDialog默認(rèn)是顯示在布
局的中間,靈活性而言popupwind更優(yōu)。
2 popupwind函數(shù)
1)構(gòu)造函數(shù)有四個
public PopupWindow (Context context)
public PopupWindow(View contentView)
public PopupWindow(View contentView, int width, int height)
public PopupWindow(View contentView, int width, int height, boolean
focusable)
3 popupwindow使用注意點(diǎn)
必須設(shè)置三個條件,①contentView ②width ③hight ,有這三個條件才能彈出。
所以popupwindow的構(gòu)造函數(shù)三個參數(shù)的是我們開發(fā)首選。
4 popupwindow顯示函數(shù)
主要使用三個:
//相對某個控件的位置(正左下方),無偏移
① showAsDropDown(View anchor):
//相對某個控件的位置,有偏移;xoff表示x軸的偏移,正值表示向左,負(fù)值表示向
右;yoff表示相對y軸的偏移,正值是向下,負(fù)值是向上;
② showAsDropDown(View anchor, int xoff, int yoff):
//相對于父控件的位置(例如下方Gravity.BOTTOM ),可以設(shè)置偏移或無偏移
③ showAtLocation(View parent, int gravity, int x, int y):
5 實(shí)踐Demo
需求就是,布局結(jié)構(gòu)是 viewpager + textView,點(diǎn)擊textview就從布局的底部彈
出一個popupwindow,popupwindow里面是一個listview
①適配器代碼如下:
public class mmAdapter extends BaseAdapter {
@Override
public int getCount() {
return listItems.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
MyPopupwindowViewHolder mpv;
if (convertView == null) {
convertView = View.inflate(MainActivity.this,
R.layout.popupwindow_item, null);
mpv = new MyPopupwindowViewHolder();
mpv.test = (TextView)
convertView.findViewById(R.id.tv_popuowindows);
convertView.setTag(mpv);
} else {
mpv = (MyPopupwindowViewHolder) convertView.getTag();
}
mpv.test.setText(listItems.get(position));
mpv.test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "popupwindows里面
的"+position+"條目被點(diǎn)擊了", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
return convertView;
}
}
public class MyPopupwindowViewHolder {
TextView test;
}
② TextView的點(diǎn)擊事件的代碼
//popupwind 自己的布局
View view = View.inflate(this, R.layout.popupwindowview, null);
popupWindow = new PopupWindow(view,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
popupWindow.setContentView(view);
listView = (ListView) view.findViewById(R.id.lv_listView);
listView.setAdapter(new mmAdapter());
//這是獲取父布局,讓其從父布局的下方顯示
View mainView =
LayoutInflater.from(MainActivity.this).inflate(R.layout.activity_main, null);
popupWindow.showAtLocation(mainView, Gravity.BOTTOM, 0, 0);
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。