天天看點

android開發action bar中menu菜單action overflow問題

http://blog.csdn.net/suppercoder/article/details/10212875

最近在學習action bar的内容,是直接參考android官網的資料學習。

      參考http://developer.android.com/training/basics/actionbar/adding-buttons.html 文檔學習action bar的開發過程中,遇到一個問題,經過搜尋以及查閱更詳細文檔,得到解答,現以記錄。

      根據原文的描述,根據下面配置使用menu

res/menu/main_activity_actions.xml

應該得到動作欄的圖示如圖:

android開發action bar中menu菜單action overflow問題

注意,紅框是我加上的強調。

問題所在:我根據文檔配置後,設定有部分按鈕為

附上我的配置:

android:showasaction="never"

[plain]

view plaincopyprint?

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

    <item android:id="@+id/action_search_id"  

          android:icon="@drawable/ic_action_search"  

          android:title="@string/action_search"  

          android:showasaction="ifroom" />  

    <item android:id="@+id/action_attach_id"  

          android:icon="@drawable/ic_action_attach"  

    <item android:id="@+id/action_call_id"  

          android:icon="@drawable/ic_action_call"  

          android:showasaction="never" />  

    <item android:id="@+id/action_copy_id"  

          android:icon="@drawable/ic_action_copy"  

</menu>  

配置上明顯有兩個是設定了never的顯示屬性,但出來的效果是沒有紅框所顯示的action overflow樣式。

android開發action bar中menu菜單action overflow問題

問題追蹤:從搜尋action overfolw的内容中找到一個很有價值的内容http://blog.sina.com.cn/s/blog_4ad7c2540101eh5c.html,按原文中提到:

4)action overflow  

action overflow中存放并不會頻繁用到的操作。按照官方網頁上的說法,“overflow圖示僅顯示在沒有menu硬按鍵的手機上,而對于有menu鍵的手機,  

overflow圖示是不顯示的,當使用者點選menu按鍵時彈出。”這樣的說法比較蹊跷,似乎和google敦促手機廠商及軟體開發商取消menu的行為不相比對。  

我再參考官方文檔http://developer.android.com/guide/topics/ui/actionbar.html 的确有如下描述:

the action bar provides users access to the most important action items relating to the app's current context. those that appear directly in the action bar with an icon and/or text are known as action buttons. actions that can't

fit in the action bar or aren't important enough are hidden in the action overflow. the user can reveal a list of the other actions by pressing the overflow button on the right side (or the device menu button, if available).

大意翻譯如下:

很多程式需要提供與目前内容相關的動作選項作為跳轉入口,動作欄正是給使用者提供了這些入口的功能。通過各個動作按鈕,動作欄直接呈現給使用者的是一個圖示或文字(也有可能是兩者的組合)。而那些由于各種原因(有可能是空間不夠,有可能是不太重要),不适合直接顯示在動作欄上面的動作按鈕,将會被放在隐藏動作按鈕部分。使用者可以通過點選隐藏動作按鈕來顯示被隐藏的其他按鈕(如果裝置有菜單功能鍵,顯示隐藏動作的功能将會由menu菜單功能鍵實作)

注意到,最後一句很關鍵。我查找我開發的虛拟裝置功能如圖:

android開發action bar中menu菜單action overflow問題

可以看到,的确有menu功能鍵!這麼來說,應該就解析的通了,顯示隐藏動作的功能由菜單功能鍵實作。我們直接做一下驗證,點選menu鍵,如圖所示:

android開發action bar中menu菜單action overflow問題

在下方我們看到了顯示的字元串,我原來的配置隻簡單的顯示<string name="action_search">str_action_search</string>這裡配置的字元串,對于示範沒有多大的影響。

這本來不是什麼大問題,但是僅僅一個小功能,也許也會使我們疑惑,在此解釋清楚。

繼續閱讀