android PopupWindow簡單例子

    在android中彈出框有兩種方式:AlertDialog和PopupWindow,它們的不一樣點在於:
    一、AlertDialog的位置固定,而PopupWindow的位置能夠隨意;
    二、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
相關文章
相關標籤/搜索