1. 程式人生 > >在ActionBar中,即便設定showAsAction="always",items仍然在overflow中顯示的問題

在ActionBar中,即便設定showAsAction="always",items仍然在overflow中顯示的問題

明明設定了android:showAsAction="always",但是所有的items全部都顯示在overflow中,然後在官網發現了答案。
如果你為了相容 Android 2.1 的版本使用了 Support 庫,在 android 名稱空間下showAsAction 屬性是不可用的。Support 庫會提供替代它的屬性,你必須宣告自己的 XML 名稱空間,並且使用該名稱空間作為屬性字首。(一個自定義 XML 名稱空間需要以你的 app 名稱為基礎,但是可以取任何你想要的名稱,它的作用域僅僅在你宣告的檔案之內。)
新增此名稱空間 xmlns:app="http://schemas.android.com/apk/res-auto" ,使用app:showAsAction代替android:showAsAction。
例如:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <menu xmlns:android="http://schemas.android.com/apk/res/android" 
 3 xmlns:app="http://schemas.android.com/apk/res-auto" 
 4 >
 5 
 6 <item
 7 android:id="@+id/action_search"
 8 android:icon="@drawable/peasy"
 9 app:showAsAction="always"
10 android:title="@string/action_search"/>
11 
12 <!-- 設定, 在溢位選單中展示 -->
13 <item
14 android:id="@+id/action_settings"
15 android:showAsAction="never"
16 android:title="@string/action_settings"/>
17 <item
18 android:id="@+id/action_about"
19 app:showAsAction="never"
20 android:title="@string/action_about"/>
21 
22 </menu>
UI依然很醜,但是效果實現了,大家將就著看吧。 煙雨林-關注程式設計師的IT科技部落格 另外,意外發現在actionBar只顯示了icon但是沒有顯示title,這是怎麼回事呢?於是又在官網深挖了....看到了這一段... https://developer.android.com/guide/topics/ui/actionbar.html#Adding
If your menu item supplies both a title and an icon—with the title and icon attributes—then the action item shows only the icon by default. If you want to display the text title, add "withText" to the showAsAction attribute. For example:
<item yourapp:showAsAction="ifRoom|withText" ... />
Note: The "withText" value is a hint to the action bar that the text title should appear. The action bar will show the title when possible, but might not if an icon is available and the action bar is constrained for space.
You should always define the title for each item even if you don't declare that the title appear with the action item, for the following reasons:
If there's not enough room in the action bar for the action item, the menu item appears in the overflow where only the title appears.
Screen readers for sight-impaired users read the menu item's title.
If the action item appears with only the icon, a user can long-press the item to reveal a tool-tip that displays the action title.
The icon is optional, but recommended. For icon design recommendations, see the Iconography design guide. You can also download a set of standard action bar icons (such as for Search or Discard) from the Downloads page.
You can also use "always" to declare that an item always appear as an action button. However, you should not force an item to appear in the action bar this way. Doing so can create layout problems on devices with a narrow screen. It's best to instead use "ifRoom" to request that an item appear in the action bar, but allow the system to move it into the overflow when there's not enough room. However, it might be necessary to use this value if the item includes an action view that cannot be collapsed and must always be visible to provide access to a critical feature.
 簡而言之,如果同時設定了icon和title,預設只會顯示icon。 如果想同時顯示title和icon,可以加入app:showAsAction="always|withText",但是即便這樣也不會一定生效,withText對actionBar的title來說只是一個hint,在條件允許的情況下actionBar會顯示title,但是當設定了icon並由於空間限制也不會顯示title。不過,官方還是建議我們設定title的,在長按icon的情況下title就會出現,另外官方還建議showAsAction最好設定為ifRoom,如果設定為always可能會在比較窄的螢幕上帶來佈局的問題。  本文地址:   http://www.yanyulin.info/pages/2014/12/1601231810102.html