pull/48/head
kunfei 5 years ago
parent 51e1860a5c
commit e3fc64c85f
  1. 5
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 34
      app/src/main/java/io/legado/app/utils/MenuExtensions.kt

@ -10,6 +10,7 @@ import io.legado.app.R
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.lib.theme.ColorUtils import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.primaryColor import io.legado.app.lib.theme.primaryColor
import io.legado.app.utils.applyOpenTint
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.disableAutoFill import io.legado.app.utils.disableAutoFill
import io.legado.app.utils.hideSoftInput import io.legado.app.utils.hideSoftInput
@ -47,6 +48,10 @@ abstract class BaseActivity(private val layoutID: Int, private val fullScreen: B
} ?: super.onCreateOptionsMenu(menu) } ?: 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 { open fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
return super.onCreateOptionsMenu(menu) return super.onCreateOptionsMenu(menu)

@ -1,13 +1,17 @@
package io.legado.app.utils package io.legado.app.utils
import android.content.Context import android.content.Context
import android.graphics.PorterDuff
import android.view.Menu import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.view.menu.MenuItemImpl import androidx.appcompat.view.menu.MenuItemImpl
import androidx.core.view.forEach import androidx.core.view.forEach
import io.legado.app.R import io.legado.app.R
import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.DrawableUtils
import io.legado.app.lib.theme.primaryTextColor 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 -> fun Menu.applyTint(context: Context, inPrimary: Boolean = true): Menu = this.let { menu ->
if (menu is MenuBuilder) { if (menu is MenuBuilder) {
@ -28,3 +32,33 @@ fun Menu.applyTint(context: Context, inPrimary: Boolean = true): Menu = this.let
} }
return menu 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) {
}
}
}
Loading…
Cancel
Save