From 39677c51b1582a1353de3909e6550e0530027282 Mon Sep 17 00:00:00 2001 From: zengfantian Date: Tue, 3 Mar 2015 00:52:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=98=BE=E7=A4=BAActionbar-O?= =?UTF-8?q?verFlowMenu=E8=8F=9C=E5=8D=95=E5=9B=BE=E6=A0=87=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zftlive/android/base/BaseActivity.java | 74 +++++++++++++++---- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/src/com/zftlive/android/base/BaseActivity.java b/src/com/zftlive/android/base/BaseActivity.java index 88cfa4d..90ecbb6 100644 --- a/src/com/zftlive/android/base/BaseActivity.java +++ b/src/com/zftlive/android/base/BaseActivity.java @@ -1,18 +1,21 @@ package com.zftlive.android.base; import java.lang.ref.WeakReference; +import java.lang.reflect.Field; +import java.lang.reflect.Method; -import android.app.ActionBar; import android.app.Activity; +import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; import android.view.MenuItem; import android.view.View; +import android.view.ViewConfiguration; +import android.view.Window; import com.zftlive.android.MApplication; -import com.zftlive.android.R; -import com.zftlive.android.common.ActionBarManager; /** * android 系统中的四大组件之一Activity基类 @@ -38,6 +41,10 @@ public abstract class BaseActivity extends Activity implements IBaseActivity{ super.onCreate(savedInstanceState); Log.d(TAG, "BaseActivity-->onCreate()"); + //设置渲染视图View + mContextView = LayoutInflater.from(this).inflate(bindLayout(), null); + setContentView(mContextView); + //获取应用Application mApplication = (MApplication)getApplicationContext(); @@ -48,15 +55,14 @@ public abstract class BaseActivity extends Activity implements IBaseActivity{ //实例化共通操作 mBaseOperation = new Operation(this); - //设置渲染视图View - mContextView = LayoutInflater.from(this).inflate(bindLayout(), null); - setContentView(mContextView); - //初始化控件 initView(mContextView); //业务操作 doBusiness(this); + + //显示VoerFlowMenu + displayOverflowMenu(getContext()); } @Override @@ -99,6 +105,42 @@ public abstract class BaseActivity extends Activity implements IBaseActivity{ mApplication.removeTask(context); } + /** + * 显示Actionbar菜单图标 + */ + @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 (Exception e) { + Log.e(TAG, "onMenuOpened-->"+e.getMessage()); + } + } + } + return super.onMenuOpened(featureId, menu); + } + + /** + * 显示OverFlowMenu按钮 + * @param mContext 上下文Context + */ + public void displayOverflowMenu(Context mContext) { + try { + ViewConfiguration config = ViewConfiguration.get(mContext); + Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); + if(menuKeyField != null) { + menuKeyField.setAccessible(true); + menuKeyField.setBoolean(config, false);//显示 + } + } catch (Exception e) { + Log.e("ActionBar", e.getMessage()); + } + } + /** * 获取当前Activity * @return @@ -117,13 +159,13 @@ public abstract class BaseActivity extends Activity implements IBaseActivity{ return this.mBaseOperation; } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case android.R.id.home: - finish(); - break; - } - return super.onOptionsItemSelected(item); - } +// @Override +// public boolean onOptionsItemSelected(MenuItem item) { +// switch (item.getItemId()) { +// case android.R.id.home: +// finish(); +// break; +// } +// return super.onOptionsItemSelected(item); +// } }