fixed action bar theme

This commit is contained in:
Mariotaku Lee 2015-04-28 12:47:57 +08:00
parent bf30ed5a35
commit a8ab005aae
6 changed files with 83 additions and 45 deletions

View File

@ -1268,4 +1268,22 @@ public class ThemeUtils implements Constants {
return ((LinkHandlerActivity) activity).peekActionBarToolbar(); return ((LinkHandlerActivity) activity).peekActionBarToolbar();
return null; 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;
}
}
} }

View File

@ -34,7 +34,6 @@ import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate; import android.support.v7.app.AppCompatDelegate;
import android.support.v7.app.AppCompatDelegateTrojan; import android.support.v7.app.AppCompatDelegateTrojan;
import android.support.v7.internal.app.WindowDecorActionBar;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.InflateException; import android.view.InflateException;
import android.view.View; 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 (actionBarContext == null) return false;
if (context == actionBarContext) return true; if (context == actionBarContext) return true;
Context base = context; Context base = context;
@ -179,20 +178,21 @@ public class ThemedLayoutInflaterFactory implements LayoutInflaterFactory {
@Nullable @Nullable
private static Context getActionBarContext(@NonNull Activity activity) { private static Context getActionBarContext(@NonNull Activity activity) {
Context actionBarContext = null;
if (activity instanceof AppCompatActivity) { if (activity instanceof AppCompatActivity) {
final AppCompatDelegate delegate = ((AppCompatActivity) activity).getDelegate(); final AppCompatDelegate delegate = ((AppCompatActivity) activity).getDelegate();
final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate); final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate);
if (actionBar instanceof WindowDecorActionBar) if (actionBar != null) {
return actionBar.getThemedContext(); actionBarContext = actionBar.getThemedContext();
}
} else if (activity instanceof AppCompatPreferenceActivity) { } else if (activity instanceof AppCompatPreferenceActivity) {
final AppCompatDelegate delegate = ((AppCompatPreferenceActivity) activity).getDelegate(); final AppCompatDelegate delegate = ((AppCompatPreferenceActivity) activity).getDelegate();
final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate); final ActionBar actionBar = AppCompatDelegateTrojan.peekActionBar(delegate);
if (actionBar instanceof WindowDecorActionBar) if (actionBar != null) {
return actionBar.getThemedContext(); actionBarContext = actionBar.getThemedContext();
} else {
final android.app.ActionBar actionBar = activity.getActionBar();
if (actionBar != null) return actionBar.getThemedContext();
} }
}
if (activity != actionBarContext) return actionBarContext;
return null; return null;
} }
} }

View File

@ -24,7 +24,6 @@ import android.content.Context;
import android.content.res.Resources; import android.content.res.Resources;
import android.support.v4.view.ViewCompat; import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatCallback; 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.StandaloneActionMode;
import android.support.v7.internal.view.SupportActionModeWrapper; import android.support.v7.internal.view.SupportActionModeWrapper;
import android.support.v7.internal.widget.ActionBarContextView; import android.support.v7.internal.widget.ActionBarContextView;
@ -115,7 +114,7 @@ public class TwidereActionModeForChildListener implements NativeActionModeAwareL
final Resources.Theme baseTheme = mActivity.getTheme(); final Resources.Theme baseTheme = mActivity.getTheme();
baseTheme.resolveAttribute(android.support.v7.appcompat.R.attr.actionBarTheme, outValue, true); 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); mActionModeView = new ActionBarContextView(actionBarContext);
mActionModePopup = new PopupWindow(actionBarContext, null, mActionModePopup = new PopupWindow(actionBarContext, null,
@ -170,25 +169,6 @@ public class TwidereActionModeForChildListener implements NativeActionModeAwareL
return mActionMode; 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() { public boolean finishExisting() {
if (mActionMode != null) { if (mActionMode != null) {
mActionMode.finish(); mActionMode.finish();

View File

@ -0,0 +1,43 @@
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2015 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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);
}
}

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?><!--
<!--
~ Twidere - Twitter client for Android ~ Twidere - Twitter client for Android
~ ~
~ Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com> ~ Copyright (C) 2012-2014 Mariotaku Lee <mariotaku.lee@gmail.com>
@ -18,27 +17,25 @@
~ along with this program. If not, see <http://www.gnu.org/licenses/>. ~ along with this program. If not, see <http://www.gnu.org/licenses/>.
--> -->
<LinearLayout <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:gravity="center_vertical" android:gravity="center_vertical"
android:orientation="horizontal" android:orientation="horizontal"
tools:layout_height="?android:actionBarSize"> tools:layout_height="?actionBarSize">
<Spinner <Spinner
android:id="@+id/account_spinner" android:id="@+id/account_spinner"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_margin="@dimen/element_spacing_small" android:layout_margin="@dimen/element_spacing_small"
android:layout_weight="0" tools:listitem="@layout/spinner_item_account_icon" />
tools:listitem="@layout/spinner_item_account_icon"/>
<FrameLayout <FrameLayout
android:layout_width="0dp" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_weight="1" android:layout_toRightOf="@+id/account_spinner"
android:padding="@dimen/element_spacing_small"> android:padding="@dimen/element_spacing_small">
<EditText <EditText
@ -50,7 +47,7 @@
android:gravity="bottom" android:gravity="bottom"
android:inputType="textPersonName|textMultiLine"> android:inputType="textPersonName|textMultiLine">
<requestFocus/> <requestFocus />
</EditText> </EditText>
<org.mariotaku.twidere.view.themed.TintThemedImageButton <org.mariotaku.twidere.view.themed.TintThemedImageButton
@ -62,7 +59,7 @@
android:color="?android:textColorPrimary" android:color="?android:textColorPrimary"
android:padding="@dimen/element_spacing_normal" android:padding="@dimen/element_spacing_normal"
android:scaleType="centerInside" android:scaleType="centerInside"
android:src="@drawable/ic_action_search"/> android:src="@drawable/ic_action_search" />
</FrameLayout> </FrameLayout>
</LinearLayout> </RelativeLayout>

View File

@ -35,11 +35,11 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical"> android:orientation="vertical">
<android.support.v7.internal.widget.ActionBarContainer <org.mariotaku.twidere.view.TwidereActionBarContainer
android:id="@+id/twidere_action_bar_container" android:id="@+id/twidere_action_bar_container"
style="?attr/actionBarStyle" style="?attr/actionBarStyle"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="?actionBarSize"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:gravity="top" android:gravity="top"
android:touchscreenBlocksFocus="true" android:touchscreenBlocksFocus="true"
@ -60,7 +60,7 @@
android:theme="?attr/actionBarTheme" android:theme="?attr/actionBarTheme"
android:visibility="gone" /> android:visibility="gone" />
</android.support.v7.internal.widget.ActionBarContainer> </org.mariotaku.twidere.view.TwidereActionBarContainer>
<View <View
android:id="@+id/window_overlay" android:id="@+id/window_overlay"