安卓學習筆記:使用PopupWindow建立簡單菜單

來源:互聯網
上載者:User

PopupWindow是一個彈出式視窗,它可以展示任意View。他會浮在當前視窗的上方展示。

下面看代碼:

public class MyActivity extends Activity

{

    private PopupWindow menu;

    private LayoutInflater inflater;

    private View layout;

 

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        //執行個體化PopupWindow建立菜單

        initMenu();

    }

 

    //判斷按鍵 菜單的顯示與隱藏

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if(!menu.isShowing()&&keyCode == KeyEvent.KEYCODE_MENU){

           show();

        }else{

            menu.dismiss();

        }

        if(keyCode == KeyEvent.KEYCODE_BACK&&menu.isShowing()){

            menu.dismiss();

        }

        return true;

    }

    //執行個體化PopupWindow建立菜單

    private void initMenu(){

 

            //擷取LayoutInflater執行個體

            inflater  = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);

            //擷取快顯功能表的布局

            layout = inflater.inflate(R.layout.menu,null);

            //設定popupWindow的布局

            menu = new PopupWindow(layout, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT);

 

    }

 

    //顯示菜單

    private void show(){

           //設定位置

            menu.showAtLocation(this.findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0); //設定在螢幕中的顯示位置

    }

}

複製代碼

菜單的布局檔案:

<?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:orientation="horizontal"

        >

    <ImageView

        android:id="@+id/icon_1"

        android:background="@drawable/icon_1"

        android:layout_width="40dp"

        android:layout_height="40dp"/>

    <ImageView

        android:id="@+id/icon_2"

        android:background="@drawable/icon_2"

        android:layout_width="40dp"

        android:layout_height="40dp"/>

    <ImageView

        android:id="@+id/icon_3"

        android:background="@drawable/icon3"

        android:layout_width="40dp"

        android:layout_height="40dp"/>

    <ImageView

        android:id="@+id/icon_4"

        android:background="@drawable/icon4"

        android:layout_width="40dp"

        android:layout_height="40dp"/>

 

</LinearLayout>

複製代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.