android 컨 텍스트 메뉴 설명

9039 단어 android
본문 은 xml 를 사용 하여 컨 텍스트 메뉴 를 만 듭 니 다.
<?xml version="1.0" encoding="utf-8"?>



<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item

            android:id="@+id/add"

            android:title="  "

            android:orderInCategory="4"

            />



    <item

            android:id="@+id/delete"

            android:title="  "

            android:orderInCategory="1"

            />



    <item

            android:id="@+id/query"

            android:title="  "

            android:orderInCategory="2"

            />



    <item

            android:id="@+id/update"

            android:title="  "

            android:orderInCategory="3"

            />

</menu>

orderInCategory 속성 은 정렬 순서 입 니 다.
 
MyActivity 의 레이아웃:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

              android:orientation="vertical"

              android:layout_width="fill_parent"

              android:layout_height="fill_parent"

        >



    <Button

            android:id="@+id/btn"

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:text="       "

            />

</LinearLayout>

 
MyActivity.java
package com.example.contextMenuTest;



import android.app.Activity;

import android.os.Bundle;

import android.view.ContextMenu;

import android.view.MenuItem;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;



public class MyActivity extends Activity {

   private Button button;



    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);



        button = (Button) findViewById(R.id.btn);



        //     contextMenu,             onCreateContextMenu  

        registerForContextMenu(button);

    }



    /**

     *   :       contextMenu   ,         

     */

    @Override

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {

        System.out.println("com.example.contextMenuTest.MyActivity.onCreateContextMenu");

        getMenuInflater().inflate(R.menu.basic_main, menu);

        //        

        menu.setHeaderIcon(R.drawable.ic_launcher);

        //        

        menu.setHeaderTitle("     ");

    }



    @Override

    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.add:

                Toast.makeText(this, "      !", 1).show();

                break;

            case R.id.delete:

                Toast.makeText(this, "      !", 1).show();

                break;

            case R.id.update:

                Toast.makeText(this, "      !", 1).show();

                break;

            case R.id.query:

                Toast.makeText(this, "      !", 1).show();

                break;

        }

        return true;

    }

}

좋은 웹페이지 즐겨찾기