ActionBar_强制显示overflow button

1.Android中的ActionBar中的那三个点的按钮,专业名字叫做:overflow button或overflow menu

2.overflow在新的Android 3.0+的系统中,默认是不显示的:

对应的:

  • 对于很多PAD来说:ActionBar中空间足够显示的话,那么对应各个menu菜单,都直接显示在ActionBar中;
  • 对于很多手机来说:ActionBar中没有足够的控件显示所有的菜单的话,余下的菜单,就被藏起来了->只有有物理菜单(MENU)键的Android设备,点击MENU键,才能出现多余的菜单;

3.想要让overflow始终都显示的话:

 

先去添加别的高手破解后强制overflow显示的那段代码getOverflowMenu,加到Activity的onCreate中:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public  class  XxxActivity{
 
     @Override
     protected  void  onCreate(Bundle savedInstanceState) {
         ...
         
         getOverflowMenu();
     }
 
     //force to show overflow menu in actionbar for android 4.4 below
     private  void  getOverflowMenu() {
          try  {
             ViewConfiguration config = ViewConfiguration.get( this );
             Field menuKeyField = ViewConfiguration. class .getDeclaredField( "sHasPermanentMenuKey" );
             if (menuKeyField !=  null ) {
                 menuKeyField.setAccessible( true );
                 menuKeyField.setBoolean(config,  false );
             }
         catch  (Exception e) {
             e.printStackTrace();
         }
     }
}

 

然后接下来,要根据ActionBar是否有足够空间显示所有menu菜单,来决定menu的android:showAsAction是否添加ifRoom:

 

  • 对于很多PAD来说:ActionBar中空间足够显示的话,那么对应的menu菜单,只有不设置为ifRoom,然后才可以被放到overflow中:
    • 举例:
      ?
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      < menu xmlns:android = "http://schemas.android.com/apk/res/android" >
       
           < item
               android:id = "@+id/menu_discard"
               android:icon = "@drawable/error_white"
               android:orderInCategory = "1"
               android:showAsAction = "ifRoom|withText"
               android:title = "@string/discard" />
           < item
               android:id = "@+id/menu_send"
               android:icon = "@drawable/forward_white"
               android:orderInCategory = "2"
               android:showAsAction = "ifRoom|withText"
               android:title = "@string/send" />
       
           < item
               android:id = "@+id/menu_settings"
               android:icon = "@drawable/settings"
               android:orderInCategory = "3"
               android:showAsAction = "withText"
               android:title = "@string/settings" />
      </ menu >
    • 对于menu_settings是可以被放到overflow按钮中的。
  • 对于很多手机来说:ActionBar中没有足够的空间显示所有的菜单的话,即使是设置了ifRoom,很多菜单本来也也还是无法全部显示,所以:即使设置了ifRoom的menu的item,以及没有设置ifRoom的menu的item,都会被放到overflow中的
  • 举例:
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    < menu xmlns:android = "http://schemas.android.com/apk/res/android"  >
     
         < item
             android:id = "@+id/menu_discard"
             android:icon = "@drawable/error_white"
             android:orderInCategory = "1"
             android:showAsAction = "ifRoom|withText"
             android:title = "@string/discard" />
         < item
             android:id = "@+id/menu_send"
             android:icon = "@drawable/forward_white"
             android:orderInCategory = "2"
             android:showAsAction = "ifRoom|withText"
             android:title = "@string/send" />
     
         < item
             android:id = "@+id/menu_settings"
             android:icon = "@drawable/settings"
             android:orderInCategory = "3"
             android:showAsAction = "withText"
             android:title = "@string/settings" />
    </ menu >
  • 其中:
  • 如果手机宽度不多,只能显示一个菜单按钮的话,那么ActionBar中只显示menu_discard,余下的,虽然已经设置了ifRoom的menu_send,和没有设置ifRoom的menu_settings,都会被放到overflow中。
ActionBar 自动生成 http://jgilfelt.github.io/android-actionbarstylegenerator/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值