当前位置: 代码迷 >> Android >> Android之ActionBar(2)
  详细解决方案

Android之ActionBar(2)

热度:68   发布时间:2016-04-28 00:22:21.0
Android之ActionBar(二)

鹰击天风壮,鹏飞海浪春。


本讲内容:ActionBar


示例一:

  

下面是res/menu/main.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.example.weixindemo.MainActivity" >    <item        android:id="@+id/action_search"        android:actionViewClass="android.widget.SearchView"        android:icon="@drawable/actionbar_search_icon"        android:showAsAction="ifRoom|collapseActionView"        android:title="@string/action_search"/>    <item        android:id="@+id/action_plus"        android:actionProviderClass="com.example.weixindemo.PlusActionProvider"        android:icon="@drawable/actionbar_add_icon"        android:showAsAction="ifRoom"        android:title="@string/action_plus"/>    <item        android:id="@+id/action_album"        android:icon="@drawable/ofm_photo_icon"        android:title="@string/action_album"/>    <item        android:id="@+id/action_collection"        android:icon="@drawable/ofm_collect_icon"        android:title="@string/action_collection"/>    <item        android:id="@+id/action_card"        android:icon="@drawable/ofm_card_icon"        android:title="@string/action_card"/>    <item        android:id="@+id/action_settings"        android:icon="@drawable/ofm_setting_icon"        android:title="@string/action_settings"/>    <item        android:id="@+id/action_feed"        android:icon="@drawable/ofm_feedback_icon"        android:title="@string/action_feed"/></menu>

下面是ActionProvider.java文件

public class PlusActionProvider extends ActionProvider {	private Context context;	public PlusActionProvider(Context context) {		super(context);		this.context = context;	}	public View onCreateActionView() {		return null;	}	public void onPrepareSubMenu(SubMenu subMenu) {		subMenu.clear();		subMenu.add(context.getString(R.string.plus_group_chat))				.setIcon(R.drawable.ofm_group_chat_icon)				.setOnMenuItemClickListener(new OnMenuItemClickListener() {					public boolean onMenuItemClick(MenuItem item) {						return true;					}				});		subMenu.add(context.getString(R.string.plus_add_friend))				.setIcon(R.drawable.ofm_add_icon)				.setOnMenuItemClickListener(new OnMenuItemClickListener() {					@Override					public boolean onMenuItemClick(MenuItem item) {						return false;					}				});		subMenu.add(context.getString(R.string.plus_video_chat))				.setIcon(R.drawable.ofm_video_icon)				.setOnMenuItemClickListener(new OnMenuItemClickListener() {					@Override					public boolean onMenuItemClick(MenuItem item) {						return false;					}				});		subMenu.add(context.getString(R.string.plus_scan))				.setIcon(R.drawable.ofm_qrcode_icon)				.setOnMenuItemClickListener(new OnMenuItemClickListener() {					@Override					public boolean onMenuItemClick(MenuItem item) {						return false;					}				});		subMenu.add(context.getString(R.string.plus_take_photo))				.setIcon(R.drawable.ofm_camera_icon)				.setOnMenuItemClickListener(new OnMenuItemClickListener() {					@Override					public boolean onMenuItemClick(MenuItem item) {						return false;					}				});	}		public boolean hasSubMenu() {		return true;	}}

下面是MainActivity.java主界面文件:

public class MainActivity extends Activity {	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		setOverflowShow();	}	public boolean onCreateOptionsMenu(Menu menu) {		getMenuInflater().inflate(R.menu.main, menu);		return true;	}	/**	 * 设置Overflow中的选项显示图标	 */	public boolean onMenuOpened(int featureId, Menu menu) {		if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {			if (menu.getClass().getSimpleName().equals("MenuBuilder")) {				try {					Method m = menu.getClass().getDeclaredMethod(							"setOptionalIconsVisible", Boolean.TYPE);					m.setAccessible(true);					m.invoke(menu, true);				} catch (Exception e) {				}			}		}		return super.onMenuOpened(featureId, menu);	}	/**	 * 设置Overflow按钮总是显示	 */	private void setOverflowShow() {		try {			ViewConfiguration config = ViewConfiguration.get(this);			Field menuKeyField = ViewConfiguration.class					.getDeclaredField("sHasPermanentMenuKey");			menuKeyField.setAccessible(true);			menuKeyField.setBoolean(config, false);		} catch (Exception e) {			e.printStackTrace();		}	}}

下面是res/values/styles.xml文件

 <style name="CustomActionBarTheme" parent="@android:style/Theme.Holo.Light">        <item name="android:actionBarStyle">@style/MyActionBar</item>        <item name="android:itemTextAppearance">@style/MyActionBarOverflow</item>        <item name="android:actionOverflowButtonStyle">@style/MyActionButtonOverflow</item>        <item name="android:itemBackground">@drawable/actionbar_bg_selector</item><!-- 子菜单背景色 -->      <!--   <item name="android:actionBarItemBackground">@drawable/actionbar_bg_selector</item>  显示按钮背景色 -->    </style>    <style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar">        <item name="android:background">#f4842d</item>        <item name="android:titleTextStyle">@style/MyActionBarTitleText</item>    </style>    <style name="MyActionBarTitleText" parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">          <item name="android:textColor">#cfcfcf</item>          <item name="android:textSize">17sp</item>      </style>     <style name="MyActionBarOverflow" parent="@android:style/Widget.ActionButton.Overflow">        <item name="android:textSize">16sp</item>        <item name="android:textColor">#cfcfcf</item>          <!-- 菜单里面的文字 -->    </style>    <style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.ActionButton.Overflow">        <item name="android:src">@drawable/actionbar_more_icon</item>        <!-- 更改overflow按钮图标 -->    </style>

下面是AndroidManifest.xml文件

<application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:logo="@drawable/logo"        android:theme="@style/CustomActionBarTheme" >



Take your time and enjoy it 

  相关解决方案