From a8ab005aaeb1779be032fffbab96f5a0c4ee6ba9 Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 28 Apr 2015 12:47:57 +0800 Subject: [PATCH] fixed action bar theme --- .../mariotaku/twidere/util/ThemeUtils.java | 18 ++++++++ .../util/ThemedLayoutInflaterFactory.java | 18 ++++---- .../TwidereActionModeForChildListener.java | 22 +--------- .../view/TwidereActionBarContainer.java | 43 +++++++++++++++++++ ...ionbar_custom_view_message_user_picker.xml | 21 ++++----- .../res/layout/activity_content_fragment.xml | 6 +-- 6 files changed, 83 insertions(+), 45 deletions(-) create mode 100644 twidere/src/main/java/org/mariotaku/twidere/view/TwidereActionBarContainer.java diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/ThemeUtils.java b/twidere/src/main/java/org/mariotaku/twidere/util/ThemeUtils.java index 566c0ba03..fe02718f0 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/ThemeUtils.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/ThemeUtils.java @@ -1268,4 +1268,22 @@ public class ThemeUtils implements Constants { return ((LinkHandlerActivity) activity).peekActionBarToolbar(); return null; } + + public static Context getActionBarThemedContext(Context base) { + final TypedValue outValue = new TypedValue(); + final Resources.Theme baseTheme = base.getTheme(); + baseTheme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarTheme, outValue, true); + + if (outValue.resourceId != 0) { + final Resources.Theme actionBarTheme = base.getResources().newTheme(); + actionBarTheme.setTo(baseTheme); + actionBarTheme.applyStyle(outValue.resourceId, true); + + final Context actionBarContext = new android.support.v7.internal.view.ContextThemeWrapper(base, 0); + actionBarContext.getTheme().setTo(actionBarTheme); + return actionBarContext; + } else { + return base; + } + } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/ThemedLayoutInflaterFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/ThemedLayoutInflaterFactory.java index 96262956b..1a241c603 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/ThemedLayoutInflaterFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/ThemedLayoutInflaterFactory.java @@ -34,7 +34,6 @@ import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatDelegate; import android.support.v7.app.AppCompatDelegateTrojan; -import android.support.v7.internal.app.WindowDecorActionBar; import android.util.AttributeSet; import android.view.InflateException; import android.view.View; @@ -167,7 +166,7 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory { } } - private static boolean isActionBarContext(Context context, Context actionBarContext) { + private static boolean isActionBarContext(@NonNull Context context, @Nullable Context actionBarContext) { if (actionBarContext == null) return false; if (context == actionBarContext) return true; Context base = context; @@ -179,20 +178,21 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory { @Nullable private static Context getActionBarContext(@NonNull Activity activity) { + Context actionBarContext = null; if (activity instanceof AppCompatActivity) { final AppCompatDelegate delegate = ((AppCompatActivity) activity).getDelegate(); final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate); - if (actionBar instanceof WindowDecorActionBar) - return actionBar.getThemedContext(); + if (actionBar != null) { + actionBarContext = actionBar.getThemedContext(); + } } else if (activity instanceof AppCompatPreferenceActivity) { final AppCompatDelegate delegate = ((AppCompatPreferenceActivity) activity).getDelegate(); final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate); - if (actionBar instanceof WindowDecorActionBar) - return actionBar.getThemedContext(); - } else { - final android.app.ActionBar actionBar = activity.getActionBar(); - if (actionBar != null) return actionBar.getThemedContext(); + if (actionBar != null) { + actionBarContext = actionBar.getThemedContext(); + } } + if (activity != actionBarContext) return actionBarContext; return null; } } diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereActionModeForChildListener.java b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereActionModeForChildListener.java index 1f690adcc..12db5a9d5 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/TwidereActionModeForChildListener.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/TwidereActionModeForChildListener.java @@ -24,7 +24,6 @@ import android.content.Context; import android.content.res.Resources; import android.support.v4.view.ViewCompat; import android.support.v7.app.AppCompatCallback; -import android.support.v7.internal.view.ContextThemeWrapper; import android.support.v7.internal.view.StandaloneActionMode; import android.support.v7.internal.view.SupportActionModeWrapper; import android.support.v7.internal.widget.ActionBarContextView; @@ -115,7 +114,7 @@ public class TwidereActionModeForChildListener implements NativeActionModeAwareL final Resources.Theme baseTheme = mActivity.getTheme(); baseTheme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarTheme, outValue, true); - final Context actionBarContext = getActionBarThemedContext(); + final Context actionBarContext = ThemeUtils.getActionBarThemedContext(mActivity); mActionModeView = new ActionBarContextView(actionBarContext); mActionModePopup = new PopupWindow(actionBarContext, null, @@ -170,25 +169,6 @@ public class TwidereActionModeForChildListener implements NativeActionModeAwareL return mActionMode; } - private Context getActionBarThemedContext() { - final TypedValue outValue = new TypedValue(); - final Resources.Theme baseTheme = mActivity.getTheme(); - baseTheme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarTheme, outValue, true); - - if (outValue.resourceId != 0) { - final Resources.Theme actionBarTheme = mActivity.getResources().newTheme(); - actionBarTheme.setTo(baseTheme); - actionBarTheme.applyStyle(outValue.resourceId, true); - - final Context actionBarContext = new ContextThemeWrapper(mActivity, 0); - actionBarContext.getTheme().setTo(actionBarTheme); - return actionBarContext; - } else { - return mActivity; - } - - } - public boolean finishExisting() { if (mActionMode != null) { mActionMode.finish(); diff --git a/twidere/src/main/java/org/mariotaku/twidere/view/TwidereActionBarContainer.java b/twidere/src/main/java/org/mariotaku/twidere/view/TwidereActionBarContainer.java new file mode 100644 index 000000000..bac57e31e --- /dev/null +++ b/twidere/src/main/java/org/mariotaku/twidere/view/TwidereActionBarContainer.java @@ -0,0 +1,43 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2015 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.view; + +import android.content.Context; +import android.support.v7.internal.widget.ActionBarContainer; +import android.util.AttributeSet; + +import org.mariotaku.twidere.util.ThemeUtils; + +/** + * Created by mariotaku on 15/4/28. + */ +public class TwidereActionBarContainer extends ActionBarContainer { + public TwidereActionBarContainer(Context context) { + super(wrapContext(context)); + } + + public TwidereActionBarContainer(Context context, AttributeSet attrs) { + super(wrapContext(context), attrs); + } + + private static Context wrapContext(Context context) { + return ThemeUtils.getActionBarThemedContext(context); + } +} diff --git a/twidere/src/main/res/layout/actionbar_custom_view_message_user_picker.xml b/twidere/src/main/res/layout/actionbar_custom_view_message_user_picker.xml index 5274361cb..cf0d1ba95 100644 --- a/twidere/src/main/res/layout/actionbar_custom_view_message_user_picker.xml +++ b/twidere/src/main/res/layout/actionbar_custom_view_message_user_picker.xml @@ -1,5 +1,4 @@ - - - + tools:layout_height="?actionBarSize"> + tools:listitem="@layout/spinner_item_account_icon" /> - + + android:src="@drawable/ic_action_search" /> - \ No newline at end of file + \ No newline at end of file diff --git a/twidere/src/main/res/layout/activity_content_fragment.xml b/twidere/src/main/res/layout/activity_content_fragment.xml index 7ab4025ae..f42fe3a3e 100644 --- a/twidere/src/main/res/layout/activity_content_fragment.xml +++ b/twidere/src/main/res/layout/activity_content_fragment.xml @@ -35,11 +35,11 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - +