优化代码

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.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<VM : ViewModel> : AppCompatActivity() {
}
/**
* 设置MENU图标颜色
*/
override fun onCreateOptionsMenu(menu: Menu): Boolean {
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 {//overflow:展开的item
DrawableUtils.setTint(it.icon, if (it.requiresOverflow()) defaultTextColor else primaryTextColor)
(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<VM : ViewModel> : 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<VM : ViewModel> : AppCompatActivity() {
}
open fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
return true
return super.onOptionsItemSelected(item)
}
protected fun initTheme() {

@ -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)
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)
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)
}
}
}

@ -51,10 +51,9 @@ class MainActivity : BaseActivity<MainViewModel>(), 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 {

@ -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)
}
}

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

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

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

Loading…
Cancel
Save