Menu,SubMenu,popupMenu,contextMenu的xml用法

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   os   

在android3.0中引入了menu菜單,通過menu的xml檔案,我們能迅速建立產生一個菜單檔案,接下來一一介紹幾種常用的菜單。

1.menu的調用,重寫兩個方法即可實現

    @Override    public boolean onCreateOptionsMenu(Menu menu) {        // TODO Auto-generated method stub        getMenuInflater().inflate(R.menu.main, menu);//填充menu檔案夾中的main.xml菜單layout        return super.onCreateOptionsMenu(menu);    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {//對菜單的子項實現監控        // TODO Auto-generated method stub        switch (item.getItemId()) {        case R.id.add:            Toast.makeText(MainActivity.this, "add", Toast.LENGTH_SHORT).show();            break;        case R.id.edit:            Toast.makeText(MainActivity.this, "edit", Toast.LENGTH_SHORT)                    .show();            break;        case R.id.del:            Toast.makeText(MainActivity.this, "del", Toast.LENGTH_SHORT).show();            break;        }        return super.onOptionsItemSelected(item);    }

2.subMenu的用法,請看代碼和注釋

    @Override    public boolean onMenuItemSelected(int featureId, MenuItem item) {//重寫onMenuItemSelected即可實現子選項的點擊事件監控,本人在實驗過程中,發現改變主題會使子選項失效        // TODO Auto-generated method stub        switch (item.getItemId()) {        case R.id.addall:            Toast.makeText(MainActivity.this, "addall", Toast.LENGTH_SHORT)                    .show();            break;        case R.id.addsomeone:            Toast.makeText(MainActivity.this, "addsomeone", Toast.LENGTH_SHORT)                    .show();            break;        }        return super.onMenuItemSelected(featureId, item);    }

 

3.popMenu的用法

  在menu的xml中聲明;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="${packageName}.${activityClass}" ><!-- 在button中設定觸發時的方法體 -->    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginLeft="22dp"        android:layout_marginTop="45dp"        android:onClick="showpopMenu"        android:text="快顯功能表" />    <ListView        android:id="@+id/listView1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_below="@+id/button1" >    </ListView></RelativeLayout>

在activity中實現方法體,即可實現彈窗效果

    public void showpopMenu(View view) {        PopupMenu popupMenu = new PopupMenu(context, view);        popupMenu.getMenuInflater().inflate(R.menu.menu1, popupMenu.getMenu());        popupMenu.show();    }

3.contextMenu,重寫兩個方法,即可實現contextMenu效果

public class MainActivity extends Activity implements OnItemLongClickListener {    private ListView lv;    private ArrayAdapter<String> mArrayAdapter;    public Context context = MainActivity.this;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        lv.setAdapter(mArrayAdapter);        registerForContextMenu(lv);    }    void init() {        lv = (ListView) findViewById(R.id.listView1);        mArrayAdapter = new ArrayAdapter<String>(context,                android.R.layout.simple_list_item_1, getData());    }    public void showpopMenu(View view) {        PopupMenu popupMenu = new PopupMenu(context, view);        popupMenu.getMenuInflater().inflate(R.menu.menu1, popupMenu.getMenu());        popupMenu.show();    }    List<String> getData() {        List<String> mlist = new ArrayList<String>();        for (int i = 0; i < 10; i++) {            mlist.add("minfan" + i);        }        return mlist;    }        @Override    public boolean onContextItemSelected(MenuItem item) {        // TODO Auto-generated method stub        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item                .getMenuInfo();        String value = mArrayAdapter.getItem(info.position);//value值表示的是listview中的item的資訊,用於定位        switch (item.getItemId()) {        case R.id.item1:            Toast.makeText(context, "item1" + value, Toast.LENGTH_SHORT).show();            break;        case R.id.item2:            Toast.makeText(context, "item2" + value, Toast.LENGTH_SHORT).show();            break;        case R.id.item3:            Toast.makeText(context, "item3" + value, Toast.LENGTH_SHORT).show();            break;        }        return super.onContextItemSelected(item);    }    @Override    public void onCreateContextMenu(android.view.ContextMenu menu, View v,            android.view.ContextMenu.ContextMenuInfo menuInfo) {        getMenuInflater().inflate(R.menu.menu1, menu);    };}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.