From 5b5c660e02bf36ec249a3afb512bf92fb58461d1 Mon Sep 17 00:00:00 2001 From: GKF Date: Thu, 30 May 2019 14:22:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E8=8F=9C=E5=8D=95=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E9=A2=9C=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/base/BaseActivity.kt | 54 +++++++++++++++++++ 1 file changed, 54 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 0de007342..a1e6d5cc7 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -1,9 +1,17 @@ package io.legado.app.base +import android.annotation.SuppressLint +import android.graphics.PorterDuff import android.os.Bundle +import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity 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 : AppCompatActivity() { @@ -21,6 +29,52 @@ abstract class BaseActivity : 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 + 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 { item?.let { if (it.itemId == android.R.id.home) {