TitleBar theme

pull/32/head
Invinciblelee 6 years ago
parent 385843dc06
commit a7dc40cba6
  1. 36
      app/src/main/java/io/legado/app/ui/widget/TitleBar.kt
  2. 1
      app/src/main/res/layout/view_titlebar.xml

@ -40,7 +40,10 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
toolbar.subtitle = subtitle toolbar.subtitle = subtitle
} }
private val mDisplayHomeAsUp: Boolean private val displayHomeAsUp: Boolean
private val navigationIconTint: ColorStateList?
private val navigationIconTintMode: Int
private val attachToActivity: Boolean
init { init {
inflate(context, R.layout.view_titlebar, this) inflate(context, R.layout.view_titlebar, this)
@ -52,19 +55,18 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
) )
val navigationIcon = a.getDrawable(R.styleable.TitleBar_navigationIcon) val navigationIcon = a.getDrawable(R.styleable.TitleBar_navigationIcon)
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 navigationIconTintMode = a.getInt(R.styleable.TitleBar_navigationIconTintMode, 9)
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) navigationIconTint = a.getColorStateList(R.styleable.TitleBar_navigationIconTint)
navigationIconTintMode = a.getInt(R.styleable.TitleBar_navigationIconTintMode, 9)
attachToActivity = a.getBoolean(R.styleable.TitleBar_attachToActivity, true)
displayHomeAsUp = a.getBoolean(R.styleable.TitleBar_displayHomeAsUp, true)
toolbar.apply { toolbar.apply {
navigationIcon?.let { navigationIcon?.let {
this.navigationIcon = it this.navigationIcon = it
this.navigationContentDescription = navigationContentDescription this.navigationContentDescription = navigationContentDescription
wrapDrawableTint(this.navigationIcon, navigationIconTint, navigationIconTintMode)
} }
if (a.hasValue(R.styleable.TitleBar_titleTextAppearance)) { if (a.hasValue(R.styleable.TitleBar_titleTextAppearance)) {
@ -102,13 +104,15 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
a.recycle() a.recycle()
val primaryTextColor = context.getPrimaryTextColor(context.isDarkTheme())
DrawableUtils.setTint(toolbar.overflowIcon, primaryTextColor)
toolbar.setTitleTextColor(primaryTextColor)
}
override fun onAttachedToWindow() {
super.onAttachedToWindow()
if (attachToActivity) { if (attachToActivity) {
attachToActivity(context) attachToActivity(context)
} }
wrapAppTheme()
} }
fun setNavigationOnClickListener(clickListener: ((View) -> Unit)) { fun setNavigationOnClickListener(clickListener: ((View) -> Unit)) {
@ -139,11 +143,23 @@ class TitleBar(context: Context, attrs: AttributeSet?) : AppBarLayout(context, a
toolbar.setSubtitleTextAppearance(context, resId) toolbar.setSubtitleTextAppearance(context, resId)
} }
private fun wrapAppTheme() {
val primaryTextColor = context.getPrimaryTextColor(context.isDarkTheme())
DrawableUtils.setTint(toolbar.overflowIcon, primaryTextColor)
toolbar.setTitleTextColor(primaryTextColor)
if (navigationIconTint != null) {
wrapDrawableTint(toolbar.navigationIcon, navigationIconTint, navigationIconTintMode)
} else {
wrapDrawableTint(toolbar.navigationIcon, ColorStateList.valueOf(primaryTextColor), navigationIconTintMode)
}
}
private fun attachToActivity(context: Context) { private fun attachToActivity(context: Context) {
val activity = getCompatActivity(context) val activity = getCompatActivity(context)
activity?.let { activity?.let {
it.setSupportActionBar(toolbar) it.setSupportActionBar(toolbar)
it.supportActionBar?.setDisplayHomeAsUpEnabled(mDisplayHomeAsUp) it.supportActionBar?.setDisplayHomeAsUpEnabled(displayHomeAsUp)
} }
} }

@ -5,5 +5,4 @@
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay"/> app:popupTheme="@style/AppTheme.PopupOverlay"/>

Loading…
Cancel
Save