improved action mode style

This commit is contained in:
Mariotaku Lee 2015-04-17 10:54:48 +08:00
parent d7421ff1f4
commit 5797ead5fb
3 changed files with 47 additions and 18 deletions

View File

@ -86,7 +86,7 @@ public abstract class ThemedActionBarActivity extends ActionBarActivity implemen
@Override
public void onSupportActionModeStarted(android.support.v7.view.ActionMode mode) {
super.onSupportActionModeStarted(mode);
ThemeUtils.applySupportActionModeBackground(mode, this, getCurrentThemeResourceId(),
ThemeUtils.applySupportActionModeColor(mode, this, getCurrentThemeResourceId(),
getCurrentThemeColor(), getThemeBackgroundOption(), true);
}

View File

@ -59,6 +59,7 @@ import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.text.TextUtils;
@ -1452,7 +1453,7 @@ public class UserFragment extends BaseSupportFragment implements OnClickListener
toolbar.setTitleTextColor(itemColor);
toolbar.setSubtitleTextColor(itemColor);
ThemeUtils.setActionBarOverflowColor(toolbar, itemColor);
ThemeUtils.wrapToolbarMenuIcon(toolbar, itemColor, itemColor);
ThemeUtils.wrapToolbarMenuIcon(ViewUtils.findViewByType(actionBarView, ActionMenuView.class), itemColor, itemColor);
}
mPagerIndicator.updateAppearance();
}

View File

@ -34,6 +34,7 @@ import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewCompat;
import android.support.v7.internal.app.WindowDecorActionBar;
@ -41,8 +42,8 @@ import android.support.v7.internal.app.WindowDecorActionBar.ActionModeImpl;
import android.support.v7.internal.view.SupportActionModeWrapper;
import android.support.v7.internal.view.SupportActionModeWrapperTrojan;
import android.support.v7.internal.view.menu.ActionMenuItemView;
import android.support.v7.internal.widget.ActionBarContextView;
import android.support.v7.internal.widget.ActionBarOverlayLayout;
import android.support.v7.internal.widget.DecorToolbar;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.text.SpannableStringBuilder;
@ -187,19 +188,20 @@ public class ThemeUtils implements Constants {
textView.setText(builder);
}
public static void applySupportActionModeBackground(ActionMode mode, FragmentActivity activity,
int themeRes, int accentColor,
String backgroundOption, boolean outlineEnabled) {
public static void applySupportActionModeColor(ActionMode mode, FragmentActivity activity,
int themeRes, int accentColor,
String backgroundOption, boolean outlineEnabled) {
// Very dirty implementation
if (!(mode instanceof SupportActionModeWrapper) || !(activity instanceof IThemedActivity))
return;
final android.support.v7.view.ActionMode modeCompat = SupportActionModeWrapperTrojan.getWrappedObject((SupportActionModeWrapper) mode);
applySupportActionModeBackground(modeCompat, activity, themeRes, accentColor, backgroundOption, outlineEnabled);
applySupportActionModeColor(modeCompat, activity, themeRes, accentColor, backgroundOption, outlineEnabled);
}
public static void applySupportActionModeBackground(android.support.v7.view.ActionMode modeCompat,
FragmentActivity activity, int themeRes,
int accentColor, String backgroundOption, boolean outlineEnabled) {
public static void applySupportActionModeColor(android.support.v7.view.ActionMode modeCompat,
FragmentActivity activity, int themeRes,
int accentColor, String backgroundOption,
boolean outlineEnabled) {
// Very dirty implementation
if (!(modeCompat instanceof ActionModeImpl)) return;
try {
@ -215,20 +217,29 @@ public class ThemeUtils implements Constants {
if (actionBar == null) return;
final Context context = actionBar.getThemedContext();
final Field contextViewField = WindowDecorActionBar.class.getDeclaredField("mContextView");
final Field decorToolbarField = WindowDecorActionBar.class.getDeclaredField("mDecorToolbar");
contextViewField.setAccessible(true);
decorToolbarField.setAccessible(true);
final View contextView = (View) contextViewField.get(actionBar);
final DecorToolbar decorToolbar = (DecorToolbar) decorToolbarField.get(actionBar);
if (contextView == null || decorToolbar == null) return;
final Toolbar toolbar = (Toolbar) decorToolbar.getViewGroup();
if (!(contextView instanceof ActionBarContextView)) return;
final ActionBarContextView actionBarContextView = (ActionBarContextView) contextView;
final TextView actionBarTitleView = (TextView) contextView.findViewById(android.support.v7.appcompat.R.id.action_bar_title);
final TextView actionBarSubtitleView = (TextView) contextView.findViewById(android.support.v7.appcompat.R.id.action_bar_subtitle);
final ImageView actionModeCloseButton = (ImageView) contextView.findViewById(android.support.v7.appcompat.R.id.action_mode_close_button);
final ActionMenuView menuView = ViewUtils.findViewByType(contextView, ActionMenuView.class);
if (actionBarTitleView == null || actionBarSubtitleView == null || actionModeCloseButton == null || menuView == null)
return;
final int actionBarColor;
if (isDarkTheme(themeRes)) {
actionBarColor = context.getResources().getColor(R.color.background_color_action_bar_dark);
} else {
actionBarColor = accentColor;
}
toolbar.setTitleTextColor(getContrastActionBarTitleColor(context, themeRes, actionBarColor));
final int titleColor = getContrastActionBarTitleColor(context, themeRes, actionBarColor);
final int itemColor = getContrastActionBarItemColor(context, themeRes, actionBarColor);
actionBarTitleView.setTextColor(titleColor);
actionBarSubtitleView.setTextColor(titleColor);
actionModeCloseButton.setColorFilter(itemColor, Mode.SRC_ATOP);
setActionBarOverflowColor(menuView, itemColor);
ThemeUtils.wrapToolbarMenuIcon(menuView, itemColor, itemColor);
ViewUtils.setBackground(contextView, getActionBarBackground(activity, themeRes, accentColor, backgroundOption, outlineEnabled));
} catch (Exception e) {
e.printStackTrace();
@ -929,7 +940,6 @@ public class ThemeUtils implements Constants {
setActionBarTitleTextColor(window, itemColor);
}
@SuppressLint("NewApi")
public static void setActionBarOverflowColor(Toolbar toolbar, int itemColor) {
if (toolbar == null) return;
final ActionMenuView actionMenuView = ViewUtils.findViewByType(toolbar, ActionMenuView.class);
@ -949,6 +959,23 @@ public class ThemeUtils implements Constants {
drawable.setColorFilter(itemColor, Mode.SRC_ATOP);
}
public static void setActionBarOverflowColor(ActionMenuView actionMenuView, int itemColor) {
if (actionMenuView == null) return;
View overflowView = null;
for (int i = 0, j = actionMenuView.getChildCount(); i < j; i++) {
final View child = actionMenuView.getChildAt(i);
final ActionMenuView.LayoutParams lp = (ActionMenuView.LayoutParams) child.getLayoutParams();
if (lp.isOverflowButton) {
overflowView = child;
break;
}
}
if (!(overflowView instanceof ImageView)) return;
final Drawable drawable = ((ImageView) overflowView).getDrawable();
if (drawable == null) return;
drawable.setColorFilter(itemColor, Mode.SRC_ATOP);
}
public static void setActionBarSubtitleTextColor(Window window, int itemColor) {
final View actionBarView = window.findViewById(android.support.v7.appcompat.R.id.action_bar);
if (actionBarView instanceof Toolbar) {
@ -1070,7 +1097,8 @@ public class ThemeUtils implements Constants {
item.setIcon(newIcon);
}
public static void wrapToolbarMenuIcon(Toolbar view, int itemColor, int popupItemColor, int... excludeGroups) {
public static void wrapToolbarMenuIcon(@Nullable ActionMenuView view, int itemColor, int popupItemColor, int... excludeGroups) {
if (view == null) return;
final Menu menu = view.getMenu();
final int childCount = view.getChildCount();
for (int i = 0, j = menu.size(), k = 0; i < j; i++) {