更改菜单图标颜色

pull/32/head
GKF 6 years ago
parent 61a3224737
commit 5b5c660e02
  1. 54
      app/src/main/java/io/legado/app/base/BaseActivity.kt

@ -1,9 +1,17 @@
package io.legado.app.base package io.legado.app.base
import android.annotation.SuppressLint
import android.graphics.PorterDuff
import android.os.Bundle import android.os.Bundle
import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import io.legado.app.R
import io.legado.app.lib.theme.ColorUtil
import io.legado.app.lib.theme.MaterialValueHelper
import io.legado.app.lib.theme.ThemeStore
import java.util.*
abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() { abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() {
@ -21,6 +29,52 @@ abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() {
} }
/**
* 设置MENU图标颜色
*/
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val primaryTextColor =
MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(ThemeStore.primaryColor(this)))
for (i in 0 until menu.size()) {
val drawable = menu.getItem(i).icon
if (drawable != null) {
drawable.mutate()
drawable.setColorFilter(primaryTextColor, PorterDuff.Mode.SRC_ATOP)
}
}
return super.onCreateOptionsMenu(menu)
}
@SuppressLint("PrivateApi")
override fun onMenuOpened(featureId: Int, menu: Menu?): Boolean {
if (menu != null) {
//展开菜单显示图标
if (menu.javaClass.simpleName.equals("MenuBuilder", ignoreCase = true)) {
try {
var method = menu.javaClass.getDeclaredMethod("setOptionalIconsVisible", java.lang.Boolean.TYPE)
method.isAccessible = true
method.invoke(menu, true)
method = menu.javaClass.getDeclaredMethod("getNonActionItems")
val menuItems = method.invoke(menu) as ArrayList<MenuItem>
if (!menuItems.isEmpty()) {
for (menuItem in menuItems) {
val drawable = menuItem.icon
if (drawable != null) {
drawable.mutate()
drawable.setColorFilter(
resources.getColor(R.color.tv_text_default),
PorterDuff.Mode.SRC_ATOP
)
}
}
}
} catch (ignored: Exception) {
}
}
}
return super.onMenuOpened(featureId, menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean { override fun onOptionsItemSelected(item: MenuItem?): Boolean {
item?.let { item?.let {
if (it.itemId == android.R.id.home) { if (it.itemId == android.R.id.home) {

Loading…
Cancel
Save