在android中弹出框有两种方式:AlertDialog和PopupWindow,它们的不同点在于:
1、AlertDialog的位置固定,而PopupWindow的位置可以随意;
2、AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的;

先来看看PopupWindow的效果:



下面来看个简单的例子:
popup_activity.xml
<?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="horizontal" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="45dp"        android:background="@drawable/headbg"        android:gravity="center_vertical" >        <ImageView            android:id="@+id/imgBack"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentLeft="true"            android:src="@drawable/back" />        <ImageView            android:id="@+id/imgDownload"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:src="@drawable/download" />    </RelativeLayout></LinearLayout>



TestActivity.java
public class TestActivity extends Activity{    private ImageView imgBack;    private ImageView imgDownload;    private PopupWindow popupWindow;    @Override    protected void onCreate(Bundle savedInstanceState)    {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.popup_activity);        imgDownload = (ImageView) findViewById(R.id.imgDownload);        imgDownload.setOnClickListener(new OnClickListener()        {            @Override            public void onClick(View v)            {                showPopupWindow(-126, 24);            }        });        imgBack = (ImageView) findViewById(R.id.imgBack);        imgBack.setOnClickListener(new OnClickListener()        {            @Override            public void onClick(View v)            {                showAlertDialog();            }        });    }    private void showPopupWindow(int xoff, int yoff)    {        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, new String[] { "下载", "上传" });        ListView listview = new ListView(this);        listview.setAdapter(adapter);        popupWindow = new PopupWindow(this);        popupWindow.setHeight(190);        popupWindow.setWidth(180);        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.argb(50, 52, 53, 55)));        popupWindow.setOutsideTouchable(true);        popupWindow.setFocusable(true);        popupWindow.setContentView(listview);        popupWindow.showAsDropDown(imgDownload, xoff, yoff);        if (thread != null && !thread.isAlive())        {            thread.start();        }    }    Thread thread = new Thread()    {        public void run()        {            while (true)            {                try                {                    Thread.sleep(1000);                } catch (InterruptedException e)                {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                Log.e("", "AAAAA");            }        };    };    private void showAlertDialog()    {        AlertDialog dial = new AlertDialog.Builder(this).setTitle("测试").show();        if (thread != null && !thread.isAlive())        {            thread.start();        }    }}


简单例子就是这样,至于其中的第二点说到“AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的”,还不知道是什么意思,测试了下,发现没什么区别,希望知道的同学在这告诉一下

我的博客其它文章列表
http://my.oschina.net/helu

更多相关文章

  1. 浅析Android中的消息机制-解决:Only the original thread that cr
  2. [Android(安卓)NDK]Android(安卓)JNI开发例子 ---3 在JNI中实现o
  3. Android异步消息机制之Handler
  4. Android的Handler机制详解3_Looper.looper()不会卡死主线程
  5. Android调用天气预报的WebService简单例子
  6. Android之Handler用法总结
  7. Android开发之消息处理机制(一)——Handler
  8. Android异步加载图像小结 (含线程池,缓存方法)
  9. android 面试题集

随机推荐

  1. Android(安卓)Bluetooth蓝牙开发\蓝牙协
  2. Android新姿势:3D翻转效果原理
  3. Android命令行启动程序-am命令的使用-纠
  4. Android(安卓)使用ContentObserver监听短
  5. Android(安卓)驱动库的动态加载
  6. Android如何制作一个简单的登陆界面
  7. 利用html5开发android(转载)
  8. Android果然强大,连截字这种操作都集成了
  9. Android 中 drawTextOnPath 文字效果演示
  10. 关于 '@android:attr/windowEnterAnimati