设置显示两个ActionBar以及设置Menu显示图标

1、系统图标:

G:\AndroidWork\adt-bundle-windows-x86-20130917\sdk\platforms\android-18\data\res\drawable-hdpi

2、ActionBar设置

(1)设置显示两个ActionBar,并且不显示OverFlow图标:

        1)设置ActionBar溢出样式styles.xml:,同时在这里还改变了OverFlow图标(“+”)

    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
        <!-- API 14 theme customizations can go here. -->
    	<item name="android:actionOverflowButtonStyle">@style/suishouActionOverflowButtonStyle</item>
    </style>

    <style name="suishouActionOverflowButtonStyle">
        <item name="android:src">@drawable/actionbar_add_icon</item>
    </style>

      2)系统默认是不显示OverFlow图标,而希望显示OverFlow图标,并且替换该图标的Icon(在上面的styles.xml):

//利用反射改变系统的行为,强制系统显示ActionBar的Overflow的图标
	private void setOverflowButtonAlways(){
		
		try {
			ViewConfiguration config=ViewConfiguration.get(this);
			Field menuKey=ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
			menuKey.setAccessible(true);
			menuKey.setBoolean(config, false);
			
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

(2)设置Menu显示图标:

//设置menu显示Icon
	@Override
	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 (NoSuchMethodException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return super.onMenuOpened(featureId, menu);
	}

你可能感兴趣的:(android,Actionbar,Menu显示图标)