Fragment中menu菜单注意事项 [复制链接]
发表于 2014-1-14 13:24 | 来自 51CTO网页
[只看他] 楼主
以前一般都是在Activity中添加menu菜单,一般是重写onCreateOptionsMenu和onOptionsItemSelected方法。
现在用fragment用的多了,就在fragment里面添加menu菜单,也是重写了onCreateOptionsMenu和onOptionsItemSelected方法,但是发现没有效果。
好吧,看了下源代码,原来跟一个mHasMenu的boolean变量有关系
这个变量控制fragment的menu菜单添加:
现在用fragment用的多了,就在fragment里面添加menu菜单,也是重写了onCreateOptionsMenu和onOptionsItemSelected方法,但是发现没有效果。
好吧,看了下源代码,原来跟一个mHasMenu的boolean变量有关系
1 |
// If set this fragment has menu items to contribute. |
2 |
boolean mHasMenu; |
01 |
boolean performCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
02 |
boolean show = false ; |
03 |
if (!mHidden) {
|
04 |
if (mHasMenu && mMenuVisible) {
|
05 |
show = true ; |
06 |
onCreateOptionsMenu(menu, inflater); |
07 |
} |
08 |
if (mChildFragmentManager != null ) {
|
09 |
show |= mChildFragmentManager.dispatchCreateOptionsMenu(menu, inflater); |
10 |
} |
11 |
} |
12 |
return <code class="plain" style="line-height: 1.1em !important; margin: 0px !important; padding: 0px !important; border: 0px !important; outline: 0px !important; float: none !important; vertical-align: baseline !important; position: static !important; left: auto !important; top: auto !important; right: auto !important; bottom: auto !important; height: auto !important; width: auto !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; min-height: |