android PopupWindow简单例子

两盒软妹~` 提交于 2020-04-11 17:50:37
    在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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!