首页 > 代码库 > 低版本系统兼容的ActionBar(二)ActionProvider+分离式ActionBar+分离式的ActionMode
低版本系统兼容的ActionBar(二)ActionProvider+分离式ActionBar+分离式的ActionMode
这篇文章主要讲的是在低版本兼容的ActionBar中实现自定义的ActionProvider,ShareActionProvider的使用方法,如何实现分离式ActionBar,外加在分离式ActionBar上的ActionMode的效果。
一、自定义ActionProvider
建立一个类,继承android.support.v4.view.ActionProvider,然后复写里面的方法即可。主要就是初始化视图和相应点击事件。
范例一:
SettingsActionProvider.java(定义按钮的图标和功能)
package com.kale.actionbar01.provider;import android.content.Context;import android.content.Intent;import android.provider.Settings;import android.support.v4.view.ActionProvider;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageButton;import com.kale.actionbar01.R;/** * @author:Jack Tony * 感谢:http://www.bdqn.cn/news/201308/10625.shtml * @date :2014-7-28 */public class SettingsActionProvider extends ActionProvider { /** An intent for launching the system settings. */ private static final Intent sSettingsIntent = new Intent(Settings.ACTION_SETTINGS); /** Context for accessing resources. */ private final Context mContext; public SettingsActionProvider(Context context) { super(context); mContext = context; System.out.println("----初始化 SettingsActionProvider----"); } @Override public View onCreateActionView() { // Inflate the action view to be shown on the action bar. LayoutInflater layoutInflater = LayoutInflater.from(mContext); View view = layoutInflater.inflate(R.layout.setting_layout, null); ImageButton button = (ImageButton) view.findViewById(R.id.imageButton_id); // Attach a click listener for launching the system settings. button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mContext.startActivity(sSettingsIntent); } }); return view; } /** * {@inheritDoc} */ @Override public boolean onPerformDefaultAction() { // This is called if the host menu item placed in the overflow menu of the // action bar is clicked and the host activity did not handle the click. mContext.startActivity(sSettingsIntent); return true; }}
范例二:
MyActionProvider.java(里面添加了子菜单,至于Item的图片在Activity中定义)
package com.kale.actionbar01.provider;import android.content.Context;import android.support.v4.view.ActionProvider;import android.view.MenuItem;import android.view.MenuItem.OnMenuItemClickListener;import android.view.SubMenu;import android.view.View;import com.kale.actionbar01.R;public class MyActionProvider extends ActionProvider { private final Context mContext; public MyActionProvider(Context context) { super(context); mContext = context; System.out.println("----初始化 MyActionProvider----"); } @Override public View onCreateActionView() { // TODO 自动生成的方法存根 return null; } @Override public void onPrepareSubMenu(SubMenu subMenu) { super.onPrepareSubMenu(subMenu); subMenu.clear(); subMenu.add("SubMenu01").setIcon(R.drawable.ic_launcher) .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return true; } }); subMenu.add("SubMenu02").setIcon(R.drawable.ic_launcher) .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return true; } }); subMenu.add("SubMenu03").setIcon(R.drawable.ic_launcher) .setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { return true; } }); } @Override public boolean hasSubMenu() { //告诉系统,这个有子菜单 return true; }}
使用方式:
1.xml布局 res/menu
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.kale.actionbar01.MainActivity" > <item android:id="@+id/menu_item_myProvider" app:showAsAction="always" android:title="自定义" app:actionProviderClass="com.kale.actionbar01.provider.SettingsActionProvider"/></menu>
然后
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; }
2.用代码来建立
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); // 在代码中再定义一个自定义的actionProvider MenuItem setItem = menu.add("setting").setIcon(R.drawable.ic_compose); MenuItemCompat.setActionProvider(setItem, new MyActionProvider(this)); MenuItemCompat.setShowAsAction(setItem, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);return true; }
二、使用ShareActionProvider
1.先在XML中定义,在代码中定义的方式同上
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="com.kale.actionbar01.MainActivity" > <item android:id="@+id/menu_item_share_provider" app:showAsAction="ifRoom" android:title="分享" app:actionProviderClass="android.support.v7.widget.ShareActionProvider" /></menu>
2.在Activity中进行设置(得到对象,然后设置intent)
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); shareProvider = (ShareActionProvider) MenuItemCompat .getActionProvider(menu.findItem(R.id.menu_item_share_provider)); if (shareProvider == null) { System.out.println("----------------> is null"); } else { System.out.println("--------------not null-----------"); } shareProvider .setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME); shareProvider.setShareIntent(createShareIntent()); return true; } /** * @return The sharing intent. */ private Intent createShareIntent() { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, "share"); return shareIntent; }
三、将ActionBar改为分离式
感谢:http://www.xue5.com/Mobile/Mobile/738648.html
1.API > =14 , 在manifest文件中的activitiy节点或application节点中添加 uiOptions=“splitActionBarWhenNarrow”属性
2.API < 14 , 除了要添加以上属性以外,还需要在activity节点中添加如下节点:
<meta-dataandroid:name="android.support.UI_OPTIONS"android:value="http://www.mamicode.com/splitActionBarWhenNarrow">
示例代码如下:
<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.AppCompat.Light.DarkActionBar" > <activity android:name=".MainActivity" android:label="@string/app_name" android:uiOptions="splitActionBarWhenNarrow" > <meta-data android:name="android.support.UI_OPTIONS" android:value="splitActionBarWhenNarrow" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
四、在分离式的ActionBar上面实现ActionMode
这个和之前讲的一样,完全没新的东西,直接上代码。
public void buttonListener(View v) { mMode = startSupportActionMode(new AnActionModeOfEpicProportions()); } private final class AnActionModeOfEpicProportions implements ActionMode.Callback { @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { MenuItemCompat.setShowAsAction( menu.add("Save").setIcon(R.drawable.ic_compose), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction( menu.add("Search").setIcon(R.drawable.ic_search), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction( menu.add("Refresh").setIcon(R.drawable.ic_refresh), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction( menu.add("Save").setIcon(R.drawable.ic_compose), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction( menu.add("Search").setIcon(R.drawable.ic_search), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); MenuItemCompat.setShowAsAction( menu.add("Refresh").setIcon(R.drawable.ic_refresh), MenuItemCompat.SHOW_AS_ACTION_IF_ROOM); return true; } @Override public boolean onPrepareActionMode(ActionMode mode, Menu menu) { return false; } @Override public boolean onActionItemClicked(ActionMode mode, MenuItem item) { Toast.makeText(MainActivity.this, "Got click: " + item, Toast.LENGTH_SHORT).show(); mode.finish(); return true; } @Override public void onDestroyActionMode(ActionMode mode) { } }
源码下载:http://download.csdn.net/detail/shark0017/7687305