android自定义dialog实现下拉弹出框效果

来源:互联网 发布:数据仿真模型 编辑:程序博客网 时间:2024/09/21 08:14

上一篇以activity方式实现了弹出框效果,这一篇就以dialog方式实现

直接看布局代码:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="vertical"    android:background="@drawable/xiyouhui_title_dialog_bg" >    <ListView         android:id="@+id/ls_hdyy_dialogleft"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="40dp"        android:layout_marginBottom="10dp"        android:divider="#ffffff"        android:dividerHeight="1dp"></ListView></LinearLayout>

不过多说明,看实现代码:

private void showLeftDialog(){View view = LayoutInflater.from(this).inflate(R.layout.xyh_hdyy_dialogleft, null);WindowManager wm = this.getWindowManager();int width = wm.getDefaultDisplay().getWidth();//创建dialog实例,R.style.MyDialogStyleTop为样式,可以在其中加入动画效果,同上一篇activity的样式
Dialog  dialogLeft = new Dialog(this,R.style.MyDialogStyleTop);
dialogLeft.setContentView(view);dialogLeft.setCanceledOnTouchOutside(true); Window dialogWindow = dialogLeft.getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); dialogWindow.setGravity(Gravity.LEFT | Gravity.TOP); lp.x = 0; // 新位置X坐标 lp.y = 110; // 新位置Y坐标 lp.width = (int) (width*0.5); // 宽度 lp.height = WindowManager.LayoutParams.WRAP_CONTENT; // 高度 dialogWindow.setAttributes(lp); listLeft.clear();
        listLeft = getListData("ceoactivity");//list添加数据        listLeft.add("全部");   //     ListView listViewDialogLeft = (ListView) view.findViewById(R.id.ls_hdyy_dialogleft);   //     YuYueDialogAdapter adapterleft = new YuYueDialogAdapter(XiyouhuiHuoDongYuYueActivity.this, listLeft);  //      listViewDialogLeft.setAdapter(adapterleft);   //     listViewDialogLeft.setOnItemClickListener(onItemClickListenerLeft);        dialogLeft.show();}

没有采用继承Dialog类的方式实现,直接在要显示的activity中添加dialog,并为此dialog添加view,然后通过WindowManager,定义dialog的位置