ActionBar setDisplayOptions 使用詳解

Android ActionBar setDisplayOptions 使用詳解


先來看看官方文檔的說明

setDisplayOptions

setDisplayOptions Added in API level 11


ActionBar 提供了下面幾種模式,來設置導航欄的樣式

Constants
int DISPLAY_HOME_AS_UP Display the ‘home’ element such that it appears as an ‘up’ affordance. //返回圖標
int DISPLAY_SHOW_CUSTOM Show the custom view if one has been set. // 自定義View
int DISPLAY_SHOW_HOME Show ‘home’ elements in this action bar, leaving more space for other navigation elements.//顯示“主頁”元素,爲其他導航元素留出更多空間。這包括Icon和Logo
int DISPLAY_SHOW_TITLE Show the activity title and subtitle, if present. //顯示標題和副標題(如果有)
int DISPLAY_USE_LOGO Use logo instead of icon if available. // 顯示Logo
int NAVIGATION_MODE_LIST This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead. //菜單列表取代靜態標題,已經棄用
int NAVIGATION_MODE_STANDARD This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead.//標準模式,已經棄用
int NAVIGATION_MODE_TABS This constant was deprecated in API level 21. Action bar navigation modes are deprecated and not supported by inline toolbar action bars. Consider using other common navigation patterns instead. //標籤導航模式。該模式代替靜態標題文本,而是提供一系列選項卡,已經棄用

如何設置ActionBar的樣式,即通過setDisplayOptions 方法

  • public abstract void setDisplayOptions (int options)

Set display options. This changes all display option bits at once. To change a limited subset of display options, see setDisplayOptions(int, int).

Parameters
options
int: A combination of the bits defined by the DISPLAY_ constants defined in ActionBar. Value is either 0 or a combination of DISPLAY_USE_LOGO, DISPLAY_SHOW_HOME, DISPLAY_HOME_AS_UP, DISPLAY_SHOW_TITLE, DISPLAY_SHOW_CUSTOM, and android.app.ActionBar.DISPLAY_TITLE_MULTIPLE_LINES

一次性設置顯示的選項,如果只設置部分選項,則使用下面這個方法


  • public abstract void setDisplayOptions (int options, int mask)

Set selected display options. Only the options specified by mask will be changed. To change all display option bits at once, see setDisplayOptions(int).

Example: setDisplayOptions(0, DISPLAY_SHOW_HOME) will disable the DISPLAY_SHOW_HOME option. setDisplayOptions(DISPLAY_SHOW_HOME, DISPLAY_SHOW_HOME | DISPLAY_USE_LOGO) will enable DISPLAY_SHOW_HOME and disable DISPLAY_USE_LOGO.

Parameters
options
A combination of the bits defined by the DISPLAY_ constants defined in ActionBar.
mask
A bit mask declaring which display options should be changed.
選擇部分來顯示設置,僅僅有當options定義的選項,在mask中被設置才幹被顯示。也就是設置爲true。


具體使用

  • 1.默認用法

ActionBar默認會顯示出一個箭頭(DISPLAY_HOME_AS_UP),一個logo(DISPLAY_SHOW_HOME),標題(DISPLAY_SHOW_TITLE)

  • 2.使用setDisplayOptions(int options)方法

首先所有的項目都變成了false,然後options使用或運算添加設置,添加一個就設置一個爲true-顯示,比如setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE),就是設置顯示返回箭頭和標題

  • 3.使用setDisplayOptions(int options,int mask)方法

首先所有的項目都變成了false,然後options使用與運算添加設置,比如setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE, ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM)
mask和options中都有ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE,那麼就是把箭頭和標題設置爲顯示,mask中剩下的ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM 就設置爲隱藏,其他沒有提到的選項就應該爲默認的配置。
可以這樣理解,mask和option執行了&運算,同時出現的爲true,出現一個的爲false。

  • 4.使用setDisplayHomeAsUpEnabled、setDisplayShowCustomEnabled等方法,設置單個選項

例如使用setDisplayHomeAsUpEnabled(true),setDisplayShowCustomEnabled(true)等單個設置;則等同於使用方法2 setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM)
如果使用setDisplayHomeAsUpEnabled(true),setDisplayShowCustomEnabled(false),等設置,則等同於使用方法3
setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_HOME_AS_UP)


參考使用:
https://blog.csdn.net/mobilexu/article/details/41147417

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章