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 c58318df1..652976bcf 100644 --- a/app/src/main/java/io/legado/app/base/BaseActivity.kt +++ b/app/src/main/java/io/legado/app/base/BaseActivity.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.view.Menu import android.view.MenuItem import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.view.menu.MenuBuilder import androidx.appcompat.view.menu.MenuItemImpl import androidx.core.content.ContextCompat import androidx.lifecycle.ViewModel @@ -30,17 +31,29 @@ abstract class BaseActivity : AppCompatActivity() { } - /** - * 设置MENU图标颜色 - */ - override fun onCreateOptionsMenu(menu: Menu): Boolean { - val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this))) - val defaultTextColor = ContextCompat.getColor(this, R.color.tv_text_default) - for (i in 0 until menu.size()) { - (menu.getItem(i) as MenuItemImpl).let {//overflow:展开的item - DrawableUtils.setTint(it.icon, if (it.requiresOverflow()) defaultTextColor else primaryTextColor) + override fun onCreateOptionsMenu(menu: Menu?): Boolean { + return menu?.let { + if (it is MenuBuilder) { + it.setOptionalIconsVisible(true) } - } + val bool = onCompatCreateOptionsMenu(it) + val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this))) + val defaultTextColor = ContextCompat.getColor(this, R.color.tv_text_default) + for (i in 0 until menu.size()) { + (menu.getItem(i) as MenuItemImpl).let { item -> + //overflow:展开的item + DrawableUtils.setTint( + item.icon, + if (item.requiresOverflow()) defaultTextColor else primaryTextColor + ) + } + } + bool + } ?: super.onCreateOptionsMenu(menu) + } + + + open fun onCompatCreateOptionsMenu(menu: Menu): Boolean { return super.onCreateOptionsMenu(menu) } @@ -74,7 +87,7 @@ abstract class BaseActivity : AppCompatActivity() { // return super.onMenuOpened(featureId, menu) // } - override fun onOptionsItemSelected(item: MenuItem?): Boolean { + final override fun onOptionsItemSelected(item: MenuItem?): Boolean { item?.let { if (it.itemId == android.R.id.home) { supportFinishAfterTransition() @@ -85,7 +98,7 @@ abstract class BaseActivity : AppCompatActivity() { } open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { - return true + return super.onOptionsItemSelected(item) } protected fun initTheme() { diff --git a/app/src/main/java/io/legado/app/lib/theme/DrawableUtils.kt b/app/src/main/java/io/legado/app/lib/theme/DrawableUtils.kt index 08b43fc82..660448088 100644 --- a/app/src/main/java/io/legado/app/lib/theme/DrawableUtils.kt +++ b/app/src/main/java/io/legado/app/lib/theme/DrawableUtils.kt @@ -26,18 +26,22 @@ object DrawableUtils { return TransitionDrawable(drawables) } - fun setTintList(drawable: Drawable, tint: ColorStateList, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { - val wrappedDrawable = DrawableCompat.wrap(drawable) - wrappedDrawable.mutate() - DrawableCompat.setTintMode(wrappedDrawable, tintMode) - DrawableCompat.setTintList(wrappedDrawable, tint) + fun setTintList(drawable: Drawable?, tint: ColorStateList, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { + drawable?.let { + val wrappedDrawable = DrawableCompat.wrap(it) + wrappedDrawable.mutate() + DrawableCompat.setTintMode(wrappedDrawable, tintMode) + DrawableCompat.setTintList(wrappedDrawable, tint) + } } - fun setTint(drawable: Drawable, @ColorInt tint: Int, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { - val wrappedDrawable = DrawableCompat.wrap(drawable) - wrappedDrawable.mutate() - DrawableCompat.setTintMode(wrappedDrawable, tintMode) - DrawableCompat.setTint(wrappedDrawable, tint) + fun setTint(drawable: Drawable?, @ColorInt tint: Int, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { + drawable?.let { + val wrappedDrawable = DrawableCompat.wrap(it) + wrappedDrawable.mutate() + DrawableCompat.setTintMode(wrappedDrawable, tintMode) + DrawableCompat.setTint(wrappedDrawable, tint) + } } } diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 246533e56..b147d44df 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -51,10 +51,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationI } } - override fun onCreateOptionsMenu(menu: Menu): Boolean { - // Inflate the menu; this adds items to the action bar if it is present. + override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.main, menu) - return true + return super.onCompatCreateOptionsMenu(menu) } override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt index 75fa15765..428500b6e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt +++ b/app/src/main/java/io/legado/app/ui/widget/TitleBar.kt @@ -33,6 +33,8 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a toolbar.subtitle = subtitle } + private val mDisplayHomeAsUp: Boolean + init { inflate(context, R.layout.view_titlebar, this) toolbar = findViewById(R.id.toolbar) @@ -44,14 +46,15 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a val navigationContentDescription = a.getText(R.styleable.TitleBar_navigationContentDescription) val navigationIconTint = a.getColorStateList(R.styleable.TitleBar_navigationIconTint) val navigationIconTintMode = a.getInt(R.styleable.TitleBar_navigationIconTintMode, 9) - val showNavigationIcon = a.getBoolean(R.styleable.TitleBar_showNavigationIcon, true) val attachToActivity = a.getBoolean(R.styleable.TitleBar_attachToActivity, true) val titleText = a.getString(R.styleable.TitleBar_title) val subtitleText = a.getString(R.styleable.TitleBar_subtitle) + mDisplayHomeAsUp = a.getBoolean(R.styleable.TitleBar_displayHomeAsUp, true) + toolbar.apply { - if (showNavigationIcon) { - this.navigationIcon = navigationIcon + navigationIcon?.let { + this.navigationIcon = it this.navigationContentDescription = navigationContentDescription wrapDrawableTint(this.navigationIcon, navigationIconTint, navigationIconTintMode) } @@ -120,7 +123,7 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a val activity = getCompatActivity(context) activity?.let { activity.setSupportActionBar(toolbar) - activity.supportActionBar?.setDisplayHomeAsUpEnabled(true) + activity.supportActionBar?.setDisplayHomeAsUpEnabled(mDisplayHomeAsUp) } } diff --git a/app/src/main/res/menu/main.xml b/app/src/main/res/menu/main.xml index d579f6feb..ffa80409c 100644 --- a/app/src/main/res/menu/main.xml +++ b/app/src/main/res/menu/main.xml @@ -5,4 +5,11 @@ android:title="@string/action_settings" android:orderInCategory="100" app:showAsAction="never"/> + + + diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 7bbabf7f6..b60fcb652 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -1,48 +1,48 @@ - + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - - - - - + + + + + diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index c7cf14a15..0cf68e183 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -25,13 +25,13 @@