两者的区别
PopupWindow和AlertDialog最大的区别:
- AlertDialog是非阻塞线程的,AlertDialog弹出的时候,后台可以做其他事情(也即弹出对话框后程序会继续向下执行);
- PopupWindow是阻塞线程的, 这就意味着在我们退出这个弹出框之前,程序会一直等待,只有当我们调用了dismiss方法之后,PopupWindow退出,程序才会向下执行。
注意:当他们两个弹出或消失时,都不会调用Activity生命周期的任何方法注:自定义对话框可以用builder.setView(View,view)设置!注:官方不推荐直接使用Dialog创建对话框(推荐使用AlertDialog或DialogFragment),而我们公司用的对话框都是继承自Diaolog的。
控制显示位置
控制popupWindow显示位置的方法:
1、showAtLocation()显示在指定位置,有两个方法重载:
- public void showAtLocation(View parent, int gravity, int x, int y)
如showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10)
- 第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
- 第二个参数指定起始点为parent的右下角
- 第三、四个参数设置以parent的右下角为原点,向左、上各偏移10像素。
- 如果没有充足的空间显示PopupWindow,那么PopupWindow将裁剪
- public void showAtLocation(IBinder token, int gravity, int x, int y)
2、showAsDropDown()显示在一个参照物View的周围,有三个方法重载:
- public void showAsDropDown(View anchor)
- public void showAsDropDown(View anchor, int xoff, int yoff)
- public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)
以上两个方法的位移参数可以通过以下方法设置:
- 具体的值
- 通过view.getWidth()等方法获取控件大小
- 通过view.getLocationOnScreen(location)方法将view在屏幕上的位置取出并存放在数组location中,进而获取位移
注意:如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
控制对话框显示位置:通过 dialog.getWindow() 获取对话框窗口对象,对此Window进行设置后再设置给dialog即可只能对Dialog设置,对AlertDialog我还不知道怎么设置
dialog和pop演示布局
<?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="fill_parent"android:background="#fff"android:orientation="vertical" ><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog1"android:text="确定取消对话框" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog2"android:text="单选对话框" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog3"android:text="多选对话框" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog4"android:text="进度条对话框" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog5"android:text="带进度的进度条对话框" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="dialog6"android:text="自定义对话框显示位置" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="popup1"android:text="popup,出现在v的下方" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="popup2"android:text="popup,出现在任意位置" /></LinearLayout>
dialog和pop演示代码
import android.content.DialogInterface.OnClickListener;
import android.widget.LinearLayout.LayoutParams;
public class MainActivity extends Activity {
@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}//确定取消对话框public void dialog1(View view) {AlertDialog.Builder builder = new Builder(this);//对话框的创建器builder.setTitle("我是对话框");builder.setMessage("对话框显示的内容");builder.setPositiveButton("确定", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {//参数:dialog-The dialog that received the click.;which-The button that was clickedToast.makeText(MainActivity.this, "确定被点击了", 0).show();}});builder.setNegativeButton("取消", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "取消被点击了", 0).show();}});builder.setCancelable(false);//是否能不做选择,默认为truebuilder.create().show();}//单选对话框public void dialog2(View view) {AlertDialog.Builder builder = new Builder(this);builder.setTitle("请选择您的性别");final String[] items = { "男", "女", "未知" };builder.setSingleChoiceItems(items, -1, new OnClickListener() {//指定选择的是哪个,-1代表默认没有选中public void onClick(DialogInterface dialog, int which) {Toast.makeText(MainActivity.this, "您的性别:" + items[which], 0).show();dialog.dismiss();}});builder.create().show();}//多选对话框public void dialog3(View view) {AlertDialog.Builder builder = new Builder(this);builder.setTitle("请选择你最爱吃的水果");final String[] items = { "苹果", "梨", "菠萝", "香蕉", "黄瓜" };final boolean[] result = new boolean[] { true, false, true, false, false };//对应条目默认是否被选中builder.setMultiChoiceItems(items, result, new OnMultiChoiceClickListener() {public void onClick(DialogInterface dialog, int which, boolean isChecked) {String inf = "";if (isChecked) {inf = " 被选中了";} else {inf = " 被取消选中了";}Toast.makeText(MainActivity.this, items[which] + inf, 0).show();result[which] = isChecked;}});builder.setPositiveButton("提交", new OnClickListener() {public void onClick(DialogInterface dialog, int which) {StringBuffer sb = new StringBuffer();for (int i = 0; i < result.length; i++) {if (result[i]) {sb.append(items[i] + " ");}}Toast.makeText(MainActivity.this, "您选中了:" + sb.toString(), 0).show();}});builder.create().show();}//进度条对话框public void dialog4(View view) {ProgressDialog pd = new ProgressDialog(this);//不需要创建器pd.setTitle("提醒");pd.setMessage("正在加载数据...请稍等。");pd.show();}//带进度的进度条对话框public void dialog5(View view) {final ProgressDialog pd = new ProgressDialog(this);pd.setTitle("提醒");pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//当设置此属性的值后才会有进度显示pd.setMax(100);pd.setMessage("正在加载数据...请稍等。");pd.show();new Thread() {public void run() {for (int i = 0; i < 100; i++) {try {Thread.sleep(20);} catch (InterruptedException e) {}pd.setProgress(i);//改变当前进度}pd.dismiss();//当进度为100%时关闭对话框,可以在任意进程中关闭};}.start();}
//自定义对话框显示位置-----不推荐!public void dialog6(View v) {Dialog dialog = new Dialog(this);dialog.setTitle("我是对话框");Window dialogWindow = dialog.getWindow(); // 获取对话框窗口对象WindowManager.LayoutParams mLayoutParams = dialogWindow.getAttributes();//获取对话框参数对象dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);//修改对话框的布局设置mLayoutParams.x = 10;// 表示相对于【原始位置】的偏移,当为Gravity.LEFT时就表示相对左边界的偏移,正值右移,负值忽略mLayoutParams.y = 100;//当为Gravity.BOTTOM时正值上移,负值忽略mLayoutParams.width = 10;//为啥根本就没用mLayoutParams.height = 80;mLayoutParams.alpha = 0.2f; // 透明度dialogWindow.setAttributes(mLayoutParams);dialog.show();}
//弹出窗体,出现在v的下方public void popup1(View v) {TextView tv = new TextView(this);tv.setText("我是弹出窗体中的内容");tv.setTextColor(0xffff0000);tv.setGravity(Gravity.CENTER);tv.setBackgroundColor(0xff00ffff);PopupWindow popWin = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, 100);popWin.setFocusable(true);popWin.setOutsideTouchable(true);popWin.setBackgroundDrawable(new ColorDrawable(0x00ff0000));//背景和上面的综合了,这行代码是必须要有的popWin.showAsDropDown(v);}//弹出窗体,出现在任意位置,值为负时会移出可视窗口public void popup2(View v) {TextView tv = new TextView(this);tv.setText("我是弹出窗体中的内容");tv.setTextColor(0xffff0000);tv.setGravity(Gravity.CENTER);tv.setBackgroundColor(0xff00ffff);PopupWindow popWin = new PopupWindow(tv, LayoutParams.WRAP_CONTENT, 100);popWin.setFocusable(true);popWin.setOutsideTouchable(true);popWin.setBackgroundDrawable(new ColorDrawable(0x00ff0000));//背景和上面的综合了,这行代码是必须要有的popWin.showAtLocation(v, Gravity.TOP | Gravity.RIGHT, 10, 100);}}
Pop+ListView演示代码
public class MainActivity extends Activity {
private EditText et_input;//输入框private ImageView iv_more;//点击弹出下拉窗口private PopupWindow popWin;private ListView listView;//弹出框PopupWindow中的Viewprivate List<String> mList;//listView中的数据private ViewHolder holder;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);et_input = (EditText) findViewById(R.id.et_input);iv_more = (ImageView) findViewById(R.id.iv_more);initListView();
//弹出PopupWindowiv_more.setOnClickListener(new OnClickListener() {public void onClick(View v) {popWin = new PopupWindow(listView, et_input.getWidth(), LayoutParams.WRAP_CONTENT);popWin.setBackgroundDrawable(new ColorDrawable(0x00888800));//背景根本没效果的!popWin.setOutsideTouchable(true); // 点击popWin 以处的区域,自动关闭 popWinpopWin.showAsDropDown(et_input);//设置 弹出窗口显示的位置,这句代码必须放在最后,否则要么显示不出来要么有BUG}});}private void initListView() {mList = new ArrayList<String>();for (int i = 0; i < 10; i++) {mList.add("工号为:0123456789" + i);}listView = new ListView(this);listView.setBackgroundResource(R.drawable.input); //背景图为一个9path图//listView.setDivider(new ColorDrawable(0xffff0000)); //设置条目之间的分隔线listView.setDivider(new ColorDrawable(getResources().getColor(R.color.red)));listView.setDividerHeight(1);//对应android:dividerHeight,代码中只能设置为intlistView.setVerticalScrollBarEnabled(false); //不显示右侧的滚动条listView.setAdapter(new MyListAdapter());}private class MyListAdapter extends BaseAdapter {@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {if (convertView == null) {convertView = View.inflate(getApplicationContext(), R.layout.item, null);holder = new ViewHolder();holder.tv_item_delete = (ImageView) convertView.findViewById(R.id.tv_item_delete);holder.tv_item_msg = (TextView) convertView.findViewById(R.id.tv_item_msg);convertView.setTag(holder);} else holder = (ViewHolder) convertView.getTag();holder.tv_item_msg.setText(mList.get(position));//设置View每个条目中删除图标的点击事件holder.tv_item_delete.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {mList.remove(position);//删除此条目在数据源中的数据notifyDataSetChanged();//内容改变时刷新listView}});//设置整个View的点击事件convertView.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {et_input.setText(mList.get(position));popWin.dismiss();}});return convertView;}@Overridepublic int getCount() {return mList.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}}private class ViewHolder {TextView tv_item_msg;ImageView tv_item_delete;}}