搜尋此網誌

顯示具有 Menu 標籤的文章。 顯示所有文章
顯示具有 Menu 標籤的文章。 顯示所有文章

2015年9月1日 星期二

【Android】Different Fragments using different / common menu in Action Bar

ref:
http://stackoverflow.com/a/30556508
http://stackoverflow.com/a/18714611
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1120/2025.html

今天想要在不同的fragment使用特定的menu item,但有些menu item是可以共用。

1.建立一個通用menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      tools:context=".MainActivity">
    <group
        android:id="@+id/main_menu_group">
        <item
            android:id="@+id/action_refresh"
            android:icon="@drawable/ic_refresh"
            android:orderInCategory="90"
            android:title="@string/action_refresh"
            app:showAsAction="ifRoom"/>

        <item
            android:id="@+id/action_settings"
            android:orderInCategory="100"
            android:title="@string/action_settings"
            app:showAsAction="never"/>
    </group>
</menu>

先把需要通用的menu item寫在<group>外,至於範例是想製造每個fragment呈現不同的menu item。

2.建立特定的menu - menu_f1.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_search"
        android:imeOptions="actionSearch"
        android:inputType="textCapWords"
        android:orderInCategory="80"
        android:title="search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="ifRoom|collapseActionView"/>
</menu>

3.在需要擁有特定menu item的fragment的onCreateView
增加setHasOptionsMenu(true);
來告知這個fragment有另外的OptionsMenu(參考)

4.建立onCreateOptionsMenu(Menu menu, MenuInflater inflater) method,並在onCreateOptionsMenu  引用(inflate)特定的menu,並把通用的menu item顯示設為false。如下所示:
@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.menu_air, menu);

        menu.setGroupVisible(R.id.main_menu_group, false);
//        menu.findItem(R.id.action_settings).setVisible(false);
//        menu.findItem(R.id.action_refresh).setVisible(false);
        
        super.onCreateOptionsMenu(menu, inflater);

    }


  • 透過1.設定group的方式,將group內的menu item設為不顯示。
  • 或者是用menu.findItem(R....).setVisible(false);將個別menu item設為不顯示。

2014年11月25日 星期二

【Android】動態Menu - onPrepareOptionsMenu

ref:
[Android]Android動態menu選單之實作演練(onPrepareOptionsMenu)
Change options menu during runtime - invalidateOptionsMenu()
Android Developer- Menus


在運行時改變menu item的狀態 
onCreateOptionsMenu()此方法是控制進入該頁面之後首次點擊Menu時會出現的選單項目,此方法只會被呼叫一次。而onPrepareOptionsMenu()此方法是每次點擊Menu時都會被呼叫一次,所以在此方法中,判斷事件的發生後將需要變化的選單加入便可以達到動態menu選單的效果。

Where the items in your options menu appear on the screen depends on the version for which you've developed your application:
  • If you've developed your application for Android 2.3.x (API level 10) or lower, the contents of your options menu appear at the bottom of the screen when the user presses the Menu button, as shown in figure 1. When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six items, Android places the sixth item and the rest into the overflow menu, which the user can open by selecting More.
  • If you've developed your application for Android 3.0 (API level 11) and higher, items from the options menu are available in the action bar. By default, the system places all items in the action overflow, which the user can reveal with the action overflow icon on the right side of the action bar (or by pressing the device Menu button, if available). To enable quick access to important actions, you can promote a few items to appear in the action bar by adding android:showAsAction="ifRoom" to the corresponding <item> elements (see figure 2).