android弹出框之.showAsDropDown(View anchor, int xoff, int yoff):PopupWindow使用详解
2016-07-14 10:24
513 查看
android中弹出框有两种,AlertDialog和PopupWindow,PoppupWindow是悬浮在当期Activity上的,相对于AlertDialog来说,位置比较随意,可以在代码中设置弹出框要显示的位置。
PopupWindow的常用设置
.showAsDropDown(view);//相对某个控件的位置(正左下方),无偏移
.showAsDropDown(View anchor, int xoff, int yoff)://相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
.showAtLocation(contentView, Gravity.BOTTOM, 0, 0); //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
.setOutsideTouchable(true);//点击popupwindow外部消失
.setFocusable(true);//获取焦点
.setOnDismissListener(this);//加上上面两句让popupwindow自动消失
.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pupwindow));// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
下面附Popupwindow弹出框和PopupWindow做下拉列表完整例子
MainActivity完整代码
PopupWindow的常用设置
.showAsDropDown(view);//相对某个控件的位置(正左下方),无偏移
.showAsDropDown(View anchor, int xoff, int yoff)://相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
.showAtLocation(contentView, Gravity.BOTTOM, 0, 0); //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
.setOutsideTouchable(true);//点击popupwindow外部消失
.setFocusable(true);//获取焦点
.setOnDismissListener(this);//加上上面两句让popupwindow自动消失
.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pupwindow));// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
下面附Popupwindow弹出框和PopupWindow做下拉列表完整例子
MainActivity完整代码
<span style="font-size:18px;color:#006600;">public class MainActivity extends Activity implements OnItemClickListener, OnDismissListener { private Button button1;//单个popupwindow点击按钮 private TextView button2;//下拉 popupwindow点击按钮 private ListView listview; private PopupWindowAdapter adapter; private PopupWindow typePopupWindow; private boolean isPopShow = false; List<String> list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button) findViewById(R.id.button1); button2=(TextView) findViewById(R.id.button2); listview=new ListView(this); listview.setOnItemClickListener(this); adapter=new PopupWindowAdapter(this); List<String> list=getList(); adapter.setData(list); button1.setOnClickListener(new SubmitBtn1()); //button2的点击事件 button2.setOnClickListener(new SubmitBtn2()); } public class SubmitBtn1 implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub showPopupWindow(v); } } public class SubmitBtn2 implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub listview.setAdapter(adapter); initTypePopup(button2.getWidth()); if(typePopupWindow != null && !isPopShow) { typePopupWindow.showAsDropDown(button2, 2, 0); isPopShow = true; } } } private void showPopupWindow(View view) { // 一个自定义的布局,作为显示的内容 View contentView = LayoutInflater.from(this).inflate( R.layout.popupwindow, null); // 设置按钮的点击事件 Button button = (Button) contentView.findViewById(R.id.btn_pop); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "button is pressed", Toast.LENGTH_SHORT).show(); onDismiss(); } }); //这里从新设置了popupWindow的width和heigth,就会以此处为标准 PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setTouchInterceptor(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return false; // 这里如果返回true的话,touch事件将被拦截 // // 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss } }); // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框 ,我觉得这里是API的一个bug popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_pupwindow)); //相对某个控件的位置(正左下方),无偏移 // popupWindow.showAsDropDown(view);//相对某个控件的位置(正左下方),无偏移 //相对某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上; // popupWindow.showAsDropDown(View anchor, int xoff, int yoff): // 相对于父控件的位置(例如正中央Gravity.CENTER 4000 ,下方Gravity.BOTTOM等),可以设置偏移或无偏移 popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0); } //自定义一个popupwindow; private void initTypePopup(int width) { typePopupWindow = new PopupWindow(listview, width, LayoutParams.WRAP_CONTENT, true); Drawable drawable = getResources().getDrawable(R.drawable.bg_pupwindow); typePopupWindow.setBackgroundDrawable(drawable); typePopupWindow.setOutsideTouchable(true);//点击popupwindow外部消失 typePopupWindow.setFocusable(true);//获取焦点 typePopupWindow.setOnDismissListener(this);//加上上面两句让破噗噗window自动消失 } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "您选择了"+adapter.getItem(position), 0).show();//显示选择的内容 typePopupWindow.dismiss();//关闭popupwindow窗口; } //定义一个list集合 private List<String> getList(){ list=new ArrayList<String>(); for(int i=0;i<100;i++){ list.add("spinner"+i); } return list; } @Override public void onDismiss() { // TODO Auto-generated method stub isPopShow = false; } }</span>activity_main.xml
<span style="color:#33cc00;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.popupwindow.MainActivity" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单个的popupwindow" android:textSize="20sp" /> <TextView android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="popupwindow下拉列表" android:textSize="20sp" android:textColor="#123eee" /> </LinearLayout> </span>popupwindow_item.xml
<span style="color:#33cc00;"><?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffff" android:padding="10dp" android:textSize="20dp" android:id="@+id/tv_popupwindow" > </TextView> </span>popupwindow.xml
<span style="color:#33cc00;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#ffffff" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="单个popupwindow" android:textSize="30dp" android:layout_gravity="center_horizontal" /> <Button android:id="@+id/btn_pop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:layout_gravity="center_horizontal" /> </LinearLayout> </span>PopupWindowAdapter完整代码
<span style="color:#33cc00;">public class PopupWindowAdapter extends BaseAdapter{ private Context context; private List<String> list; public PopupWindowAdapter(Context context){ this.context=context; } public void setData(List<String> lists){ list=lists; this.notifyDataSetChanged(); } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub HolderView holderView=null; if(null==convertView){ holderView=new HolderView(); convertView=LayoutInflater.from(context).inflate(R.layout.popupwindow_item, parent,false); holderView.time= (TextView) convertView.findViewById(R.id.tv_popupwindow); convertView.setTag(holderView); }else{ holderView = (HolderView) convertView.getTag(); } String time=(String) getItem(position); holderView.time.setText(time); return convertView; } class HolderView { TextView time; } } </span>
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories