From e3fc64c85f74fe2b832c1ed088c95ed40596474b Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 15 Nov 2019 21:00:37 +0800 Subject: [PATCH] up --- .../java/io/legado/app/base/BaseActivity.kt | 5 +++ .../io/legado/app/utils/MenuExtensions.kt | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/app/src/main/java/io/legado/app/base/BaseActivity.kt b/app/src/main/java/io/legado/app/base/BaseActivity.kt index c02f4731a..c30e61a9c 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -10,6 +10,7 @@ import io.legado.app.R import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.primaryColor +import io.legado.app.utils.applyOpenTint import io.legado.app.utils.applyTint import io.legado.app.utils.disableAutoFill import io.legado.app.utils.hideSoftInput @@ -47,6 +48,10 @@ abstract class BaseActivity(private val layoutID: Int, private val fullScreen: B } ?: super.onCreateOptionsMenu(menu) } + override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean { + menu?.applyOpenTint(this) + return super.onMenuOpened(featureId, menu) + } open fun onCompatCreateOptionsMenu(menu: Menu): Boolean { return super.onCreateOptionsMenu(menu) diff --git a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt index d4b55caaa..b92fbea68 100644 --- a/app/src/main/java/io/legado/app/utils/MenuExtensions.kt +++ b/app/src/main/java/io/legado/app/utils/MenuExtensions.kt @@ -1,13 +1,17 @@ package io.legado.app.utils import android.content.Context +import android.graphics.PorterDuff import android.view.Menu +import android.view.MenuItem import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl import androidx.core.view.forEach import io.legado.app.R import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.primaryTextColor +import java.lang.reflect.Method +import java.util.* fun Menu.applyTint(context: Context, inPrimary: Boolean = true): Menu = this.let { menu -> if (menu is MenuBuilder) { @@ -27,4 +31,34 @@ fun Menu.applyTint(context: Context, inPrimary: Boolean = true): Menu = this.let } } return menu +} + +fun Menu.applyOpenTint(context: Context) { + //展开菜单显示图标 + if (this.javaClass.simpleName.equals("MenuBuilder", ignoreCase = true)) { + val defaultTextColor = context.getCompatColor(R.color.tv_text_default) + try { + var method: Method = + this.javaClass.getDeclaredMethod("setOptionalIconsVisible", java.lang.Boolean.TYPE) + method.isAccessible = true + method.invoke(this, true) + method = this.javaClass.getDeclaredMethod("getNonActionItems") + val menuItems = method.invoke(this) + if (menuItems is ArrayList<*>) { + for (menuItem in menuItems) { + if (menuItem is MenuItem) { + val drawable = menuItem.icon + if (drawable != null) { + drawable.mutate() + drawable.setColorFilter( + defaultTextColor, + PorterDuff.Mode.SRC_ATOP + ) + } + } + } + } + } catch (ignored: Exception) { + } + } } \ No newline at end of file