popupwindow 如何實現彈出菜單效果_popupwindow 實現彈出窗口範例

popupwindow實現彈出菜單功能非常實用,在有佈局中經常出現,給用戶體驗非常不錯,以下是我總結popupwindow實現彈出窗口的經常使用範例:

1.  先看效果:

2. main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:background="@drawable/bg_5">
    <ListView android:id="@+id/lv_id" android:layout_height="wrap_content"
        android:layout_width="match_parent" />
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:orientation="horizontal" android:layout_alignParentBottom="true"
        android:background="@drawable/title_weekend_bg">
        <Button android:id="@+id/but_menu"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="菜單" android:background="@drawable/button_select"/>
        <Button android:id="@+id/but_news"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="新聞" android:background="@drawable/button_select"
            android:layout_marginLeft="10dp"/>
       </LinearLayout>
</RelativeLayout>

3. popupmenu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="match_parent" android:layout_width="match_parent"
    android:background="@drawable/main" android:orientation="vertical">
    <TextView android:id="@+id/open_id" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="打開"/>
    <TextView android:id="@+id/save_id" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="w保存"/>
    <TextView android:id="@+id/my_id" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="我的留言"/>
    <TextView android:id="@+id/about_id" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="關於我們"/>
    <TextView android:id="@+id/help_id" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="幫助"/>
</LinearLayout>

 4. 核心java代碼:按下按鈕時顯示彈出窗口

menuBut.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater layoutInflater = getLayoutInflater();
                View view = layoutInflater.inflate(R.layout.popupmenu,null);
                //PopupWindow彈出的窗口顯示的view,第二和第三參數:分別表示此彈出窗口的大小 
                m_popupWindow = new PopupWindow(view, 150, 250);
                //PopupWindow彈出的窗口顯示的位置 ,第一個參數指在那個view上 ,第二和第三參數分別表示距離此view的x和y的距離
                m_popupWindow.showAsDropDown(m_btnOk, 0,0);
            }
        });

 5. 關閉彈出的窗口

public boolean onTouchEvent(MotionEvent event) {
        //關閉m_popupWindow窗口
        m_popupWindow.dismiss();
        return super.onTouchEvent(event);
    }

轉載自:http://www.ideaex.net/html/Article/2011/07/29/281.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章