• ActionBarSherlock学习笔记——添加组件Menu


    添加功能组件

    1.添加组件在onCreateOptionsMenu(Menu menu)中

     1 @Override
     2     public boolean onCreateOptionsMenu(Menu menu) {
     3         // Used to put dark icons on light action bar
     4         boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;
     5 
     6         menu.add("Save")
     7                 .setIcon(
     8                         isLight ? R.drawable.ic_compose_inverse
     9                                 : R.drawable.ic_compose)
    10                 .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    11 
    12         menu.add("Search").setShowAsAction(
    13                 MenuItem.SHOW_AS_ACTION_IF_ROOM
    14                         | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    15 
    16         menu.add("Refresh")
    17                 .setIcon(
    18                         isLight ? R.drawable.ic_refresh_inverse
    19                                 : R.drawable.ic_refresh)
    20                 .setShowAsAction(
    21                         MenuItem.SHOW_AS_ACTION_IF_ROOM
    22                                 | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    23 
    24         return true;
    25     }
    setIcon(int res):设置图标。
    add(String name):为新添加item的名字。
    setActionView(R.layout.xxx):添加点击视图,同时需要在setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW).

    setShowAsAction(int action):item的显示方式,如下几种:
     1 /**
     2      *  从不在ActionBar中显示,但按下menu键会在下方显示 */
     3     public static final int SHOW_AS_ACTION_NEVER = 0;
     4     /** 由系统决定是否有多余控件来显示这个item */
     5     public static final int SHOW_AS_ACTION_IF_ROOM = 1;
     6     /**
     7      * 会一直在ActionBar中显示,省着点用,多了会影响用户体验
     8      */
     9     public static final int SHOW_AS_ACTION_ALWAYS = 2;
    10 
    11     /**
    12      * 在ActionBar中显示这个item的标签,即是他有一个icon
    13      */
    14     public static final int SHOW_AS_ACTION_WITH_TEXT = 4;
    15 
    16     /**
    17      * This item's action view collapses to a normal menu item.
    18      * When expanded, the action view temporarily takes over
    19      * a larger segment of its container.点开收索出现的那个输入框
    20      */
    21     public static final int SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW = 8;

    添加item的第二种方式:

    1 /**
    2      * 解析R.menu.xxx
    3      */
    4     @Override
    5     public boolean onCreateOptionsMenu(Menu menu) {
    6         super.onCreateOptionsMenu(menu);
    7         getSupportMenuInflater().inflate(R.menu.settings_action_provider, menu);
    8         return true;
    9     }

    其中R.menu.xxx文件如下:

     1 <menu xmlns:android="http://schemas.android.com/apk/res/android" >
     2 
     3     <item
     4         android:id="@+id/menu_item_action_provider_action_bar"
     5         android:actionProviderClass="com.actionbarsherlock.sample.demos.ActionProviders$SettingsActionProvider"
     6         android:showAsAction="ifRoom"<!--设置显示的方式--!>
     7         android:title="Settings"/>
     8     <item
     9         android:id="@+id/menu_item_action_provider_overflow"
    10         android:actionProviderClass="com.actionbarsherlock.sample.demos.ActionProviders$SettingsActionProvider"
    11         android:showAsAction="never"
    12         android:title="Settings"/>
    13 
    14 </menu>
    android:actionProviderClass属性用于添加创建item,布局的类
     1 /**
     2      * 内部类用于常见OptionsItem的view布局
     3      * 
     4      * @author Administrator
     5      * 
     6      */
     7     public static class SettingsActionProvider extends ActionProvider {
     8 
     9         /** An intent for launching the system settings. */
    10         private static final Intent sSettingsIntent = new Intent(
    11                 Settings.ACTION_SETTINGS);
    12 
    13         /** Context for accessing resources. */
    14         private final Context mContext;
    15 
    16         /**
    17          * Creates a new instance.
    18          * 
    19          * @param context
    20          *            Context for accessing resources.
    21          */
    22         public SettingsActionProvider(Context context) {
    23             super(context);
    24             mContext = context;
    25         }
    26 
    27         /**
    28          * 创建布局view,并为view中的控件添加事件
    29          */
    30         @Override
    31         public View onCreateActionView() {
    32             // Inflate the action view to be shown on the action bar.
    33             LayoutInflater layoutInflater = LayoutInflater.from(mContext);
    34             View view = layoutInflater.inflate(
    35                     R.layout.settings_action_provider, null);
    36             ImageButton button = (ImageButton) view.findViewById(R.id.button);
    37             // Attach a click listener for launching the system settings.
    38             button.setOnClickListener(new View.OnClickListener() {
    39                 @Override
    40                 public void onClick(View v) {
    41                     mContext.startActivity(sSettingsIntent);
    42                 }
    43             });
    44             return view;
    45         }
    46 
    47         /**
    48          * 处理按钮menu弹出的menu的点击事件,并且主onOptionsItemSelected没有处理,这个点击事件,即返回的为false时,会调用这个方法
    49          */
    50         @Override
    51         public boolean onPerformDefaultAction() {
    52             // This is called if the host menu item placed in the overflow menu
    53             // of the
    54             // action bar is clicked and the host activity did not handle the
    55             // click.
    56             mContext.startActivity(sSettingsIntent);
    57             return true;
    58         }
    59     }

    2.监听onCreateOptionsMenu中的item点击事件

    1 /**
    2      * 监听item的点击
    3      */
    4     @Override
    5     public boolean onOptionsItemSelected(MenuItem item) {
    6         // TODO Auto-generated method stub
    7         Toast.makeText(this, "你点击了:" + item, Toast.LENGTH_LONG).show();
    8         return super.onOptionsItemSelected(item);
    9     }

    3.添加组件使用ActionMode,这个功能可以在没有ActionBar的情况下使用

     import com.actionbarsherlock.view.ActionMode;
    实例化:startActionMode(new
    myActionMode )
     1 /**
     2      * 添加AcionMode,需要实现其Callback
     3      * 
     4      * @author Administrator
     5      * 
     6      */
     7     private class myActionMode implements ActionMode.Callback {
     8         // 添加item
     9         @Override
    10         public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    11             // TODO Auto-generated method stub
    12             menu.add("New_0").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    13             menu.add("New_1").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    14             menu.add("New_2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    15             menu.add("New_3").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    16             menu.add("New_4").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    17             menu.add("New_5").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    18             menu.add("New_6").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    19             return true;
    20         }
    21 
    22         // create后会调用这个方法
    23         @Override
    24         public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    25             // TODO Auto-generated method stub
    26             menu.add("New_7").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    27             return true;
    28         }
    29 
    30         // item点击事件处理
    31         @Override
    32         public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    33             // TODO Auto-generated method stub
    34             Toast.makeText(MainActivity.this, "你点击了:" + item, Toast.LENGTH_LONG)
    35                     .show();
    36             return true;
    37         }
    38 
    39         // ActionMode消失的时候调用的方法
    40         @Override
    41         public void onDestroyActionMode(ActionMode mode) {
    42             // TODO Auto-generated method stub
    43         }
    44     }
    mode.finish();关闭这个mode
    invalidateOptionsMenu():重新调用onCreateOptionsMenu

    4.添加ContextMenu上下文菜单

     1     /**
     2      * 为view注册长按的上下文菜单
     3      */
     4     registerForContextMenu(findViewById(R.id.view0));
     5     /**
     6      * 为上下文菜单添加item
     7      */
     8     @Override
     9     public void onCreateContextMenu(ContextMenu menu, View v,
    10             ContextMenuInfo menuInfo) {
    11         // TODO Auto-generated method stub
    12         menu.add("New_0");
    13         menu.add("New_1");
    14         menu.add("New_2");
    15         menu.add("New_3");
    16         super.onCreateContextMenu(menu, v, menuInfo);
    17     }
    18 
    19     /**
    20      * 获得上下文菜单点击事件
    21      */
    22     @Override
    23     public boolean onContextItemSelected(android.view.MenuItem item) {
    24         // TODO Auto-generated method stub
    25         System.out.println(item.toString());
    26         return super.onContextItemSelected(item);
    27     }

     源码下载地址 https://github.com/JakeWharton/ActionBarSherlock

  • 相关阅读:
    centos7安装zabbix4.2
    python3.x 基础三:文件IO
    python3.x 基础三:字符集问题
    python3.x 基础三:set集合
    python3.x 基础二:内置函数
    python3.x 基础一:dict字典
    python3.x 基础一:str字符串方法
    python3.x 基础一
    [leetcode]Path Sum
    [leetcode]Balanced Binary Tree
  • 原文地址:https://www.cnblogs.com/qinghuaideren/p/3064729.html
Copyright © 2020-2023  润新知