优化代码

pull/32/head
Invinciblelee 6 years ago
parent 100b3d2216
commit cd211f87a9
  1. 29
      app/src/main/java/io/legado/app/base/BaseActivity.kt
  2. 12
      app/src/main/java/io/legado/app/lib/theme/DrawableUtils.kt
  3. 5
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  4. 11
      app/src/main/java/io/legado/app/ui/widget/TitleBar.kt
  5. 7
      app/src/main/res/menu/main.xml
  6. 2
      app/src/main/res/values/attrs.xml
  7. 2
      app/src/main/res/values/styles.xml

@ -4,6 +4,7 @@ import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.view.menu.MenuItemImpl import androidx.appcompat.view.menu.MenuItemImpl
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
@ -30,17 +31,29 @@ abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() {
} }
/** override fun onCreateOptionsMenu(menu: Menu?): Boolean {
* 设置MENU图标颜色 return menu?.let {
*/ if (it is MenuBuilder) {
override fun onCreateOptionsMenu(menu: Menu): Boolean { it.setOptionalIconsVisible(true)
}
val bool = onCompatCreateOptionsMenu(it)
val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this))) val primaryTextColor = getPrimaryTextColor(ColorUtils.isColorLight(ThemeStore.primaryColor(this)))
val defaultTextColor = ContextCompat.getColor(this, R.color.tv_text_default) val defaultTextColor = ContextCompat.getColor(this, R.color.tv_text_default)
for (i in 0 until menu.size()) { for (i in 0 until menu.size()) {
(menu.getItem(i) as MenuItemImpl).let {//overflow:展开的item (menu.getItem(i) as MenuItemImpl).let { item ->
DrawableUtils.setTint(it.icon, if (it.requiresOverflow()) defaultTextColor else primaryTextColor) //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) return super.onCreateOptionsMenu(menu)
} }
@ -74,7 +87,7 @@ abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() {
// return super.onMenuOpened(featureId, menu) // return super.onMenuOpened(featureId, menu)
// } // }
override fun onOptionsItemSelected(item: MenuItem?): Boolean { final override fun onOptionsItemSelected(item: MenuItem?): Boolean {
item?.let { item?.let {
if (it.itemId == android.R.id.home) { if (it.itemId == android.R.id.home) {
supportFinishAfterTransition() supportFinishAfterTransition()
@ -85,7 +98,7 @@ abstract class BaseActivity<VM : ViewModel> : AppCompatActivity() {
} }
open fun onCompatOptionsItemSelected(item: MenuItem): Boolean { open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
return true return super.onOptionsItemSelected(item)
} }
protected fun initTheme() { protected fun initTheme() {

@ -26,18 +26,22 @@ object DrawableUtils {
return TransitionDrawable(drawables) return TransitionDrawable(drawables)
} }
fun setTintList(drawable: Drawable, tint: ColorStateList, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { fun setTintList(drawable: Drawable?, tint: ColorStateList, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) {
val wrappedDrawable = DrawableCompat.wrap(drawable) drawable?.let {
val wrappedDrawable = DrawableCompat.wrap(it)
wrappedDrawable.mutate() wrappedDrawable.mutate()
DrawableCompat.setTintMode(wrappedDrawable, tintMode) DrawableCompat.setTintMode(wrappedDrawable, tintMode)
DrawableCompat.setTintList(wrappedDrawable, tint) DrawableCompat.setTintList(wrappedDrawable, tint)
} }
}
fun setTint(drawable: Drawable, @ColorInt tint: Int, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) { fun setTint(drawable: Drawable?, @ColorInt tint: Int, tintMode: PorterDuff.Mode = PorterDuff.Mode.SRC_ATOP) {
val wrappedDrawable = DrawableCompat.wrap(drawable) drawable?.let {
val wrappedDrawable = DrawableCompat.wrap(it)
wrappedDrawable.mutate() wrappedDrawable.mutate()
DrawableCompat.setTintMode(wrappedDrawable, tintMode) DrawableCompat.setTintMode(wrappedDrawable, tintMode)
DrawableCompat.setTint(wrappedDrawable, tint) DrawableCompat.setTint(wrappedDrawable, tint)
} }
} }
}

@ -51,10 +51,9 @@ class MainActivity : BaseActivity<MainViewModel>(), NavigationView.OnNavigationI
} }
} }
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.main, menu) menuInflater.inflate(R.menu.main, menu)
return true return super.onCompatCreateOptionsMenu(menu)
} }
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {

@ -33,6 +33,8 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
toolbar.subtitle = subtitle toolbar.subtitle = subtitle
} }
private val mDisplayHomeAsUp: Boolean
init { init {
inflate(context, R.layout.view_titlebar, this) inflate(context, R.layout.view_titlebar, this)
toolbar = findViewById(R.id.toolbar) 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 navigationContentDescription = a.getText(R.styleable.TitleBar_navigationContentDescription)
val navigationIconTint = a.getColorStateList(R.styleable.TitleBar_navigationIconTint) val navigationIconTint = a.getColorStateList(R.styleable.TitleBar_navigationIconTint)
val navigationIconTintMode = a.getInt(R.styleable.TitleBar_navigationIconTintMode, 9) 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 attachToActivity = a.getBoolean(R.styleable.TitleBar_attachToActivity, true)
val titleText = a.getString(R.styleable.TitleBar_title) val titleText = a.getString(R.styleable.TitleBar_title)
val subtitleText = a.getString(R.styleable.TitleBar_subtitle) val subtitleText = a.getString(R.styleable.TitleBar_subtitle)
mDisplayHomeAsUp = a.getBoolean(R.styleable.TitleBar_displayHomeAsUp, true)
toolbar.apply { toolbar.apply {
if (showNavigationIcon) { navigationIcon?.let {
this.navigationIcon = navigationIcon this.navigationIcon = it
this.navigationContentDescription = navigationContentDescription this.navigationContentDescription = navigationContentDescription
wrapDrawableTint(this.navigationIcon, navigationIconTint, navigationIconTintMode) wrapDrawableTint(this.navigationIcon, navigationIconTint, navigationIconTintMode)
} }
@ -120,7 +123,7 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
val activity = getCompatActivity(context) val activity = getCompatActivity(context)
activity?.let { activity?.let {
activity.setSupportActionBar(toolbar) activity.setSupportActionBar(toolbar)
activity.supportActionBar?.setDisplayHomeAsUpEnabled(true) activity.supportActionBar?.setDisplayHomeAsUpEnabled(mDisplayHomeAsUp)
} }
} }

@ -5,4 +5,11 @@
android:title="@string/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" android:orderInCategory="100"
app:showAsAction="never"/> app:showAsAction="never"/>
<item android:id="@+id/action_test"
android:title="测试"
android:icon="@drawable/ic_search"
android:orderInCategory="100"
app:showAsAction="never"/>
</menu> </menu>

@ -9,7 +9,7 @@
<attr name="subtitleTextAppearance"/> <attr name="subtitleTextAppearance"/>
<attr name="subtitleTextColor"/> <attr name="subtitleTextColor"/>
<attr name="attachToActivity" format="boolean"/> <attr name="attachToActivity" format="boolean"/>
<attr name="showNavigationIcon" format="boolean" /> <attr name="displayHomeAsUp" format="boolean"/>
<attr name="navigationIcon" format="reference"/> <attr name="navigationIcon" format="reference"/>
<attr name="navigationContentDescription" format="reference|string"/> <attr name="navigationContentDescription" format="reference|string"/>
<attr name="navigationIconTint" format="color|reference"/> <attr name="navigationIconTint" format="color|reference"/>

@ -25,13 +25,13 @@
<!-- Base application theme. --> <!-- Base application theme. -->
<style name="Base.AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar"> <style name="Base.AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
<item name="actionOverflowMenuStyle">@style/Style.PopupMenu</item> <item name="actionOverflowMenuStyle">@style/Style.PopupMenu</item>
<item name="android:itemTextAppearance">@style/Style.MenuItemText</item> <item name="android:itemTextAppearance">@style/Style.MenuItemText</item>
<item name="android:popupMenuStyle">@style/Style.PopupMenu</item> <item name="android:popupMenuStyle">@style/Style.PopupMenu</item>
<item name="android:windowTranslucentStatus">true</item>
</style> </style>
<style name="AppTheme.AppBarOverlay.Light" parent="ThemeOverlay.AppCompat.Light"> <style name="AppTheme.AppBarOverlay.Light" parent="ThemeOverlay.AppCompat.Light">

Loading…
Cancel
Save