feat: 优化代码

pull/198/head
kunfei 5 years ago
parent 17248ef6e8
commit a44d2da989
  1. 30
      app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt
  2. 5
      app/src/main/java/io/legado/app/ui/widget/prefs/PreferenceCategory.kt
  3. 4
      app/src/main/res/layout/view_preference_category.xml

@ -1,6 +1,7 @@
package io.legado.app.ui.widget.prefs
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
@ -11,11 +12,15 @@ import android.widget.TextView
import androidx.core.view.isVisible
import androidx.preference.PreferenceViewHolder
import io.legado.app.R
import io.legado.app.lib.theme.Selector
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.utils.getCompatColor
import org.jetbrains.anko.layoutInflater
import kotlin.math.roundToInt
class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Preference(context, attrs) {
class Preference(context: Context, attrs: AttributeSet) :
androidx.preference.Preference(context, attrs) {
init {
// isPersistent = true
@ -24,9 +29,22 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Pr
companion object {
fun <T: View> bindView(context: Context, it: PreferenceViewHolder?, icon: Drawable?, title: CharSequence?, summary: CharSequence?, weightLayoutRes: Int?, viewId: Int?,
weightWidth: Int = 0, weightHeight: Int = 0): T? {
fun <T : View> bindView(
context: Context,
it: PreferenceViewHolder?,
icon: Drawable?,
title: CharSequence?,
summary: CharSequence?,
weightLayoutRes: Int?,
viewId: Int?,
weightWidth: Int = 0,
weightHeight: Int = 0
): T? {
if (it == null) return null
it.itemView.background = Selector.drawableBuild()
.setDefaultDrawable(ColorDrawable(context.backgroundColor))
.setPressedDrawable(ColorDrawable(context.getCompatColor(R.color.btn_bg_press)))
.create()
val view = it.findViewById(R.id.preference_title)
if (view is TextView) {
view.text = title
@ -62,9 +80,11 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Pr
if (weightWidth > 0 || weightHeight > 0) {
val lp = lay.layoutParams
if (weightHeight > 0)
lp.height = (context.resources.displayMetrics.density * weightHeight).roundToInt()
lp.height =
(context.resources.displayMetrics.density * weightHeight).roundToInt()
if (weightWidth > 0)
lp.width = (context.resources.displayMetrics.density * weightWidth).roundToInt()
lp.width =
(context.resources.displayMetrics.density * weightWidth).roundToInt()
lay.layoutParams = lp
}

@ -8,7 +8,9 @@ import androidx.core.view.isVisible
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceViewHolder
import io.legado.app.R
import io.legado.app.lib.theme.ColorUtils
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.backgroundColor
import io.legado.app.utils.getCompatColor
@ -28,16 +30,19 @@ class PreferenceCategory(context: Context, attrs: AttributeSet) : PreferenceCate
if (view.isInEditMode) {
view.setTextColor(context.getCompatColor(R.color.colorAccent))
} else {
view.setBackgroundColor(context.backgroundColor)
view.setTextColor(context.accentColor)
}
view.isVisible = title != null && title.isNotEmpty()
val da = it.findViewById(R.id.preference_divider_above)
if (da is View) {
da.setBackgroundColor(ColorUtils.darkenColor(context.backgroundColor))
da.isVisible = it.isDividerAllowedAbove
}
val db = it.findViewById(R.id.preference_divider_below)
if (db is View) {
db.setBackgroundColor(ColorUtils.darkenColor(context.backgroundColor))
db.isVisible = it.isDividerAllowedBelow
}
}

@ -13,8 +13,8 @@
<TextView
android:id="@+id/preference_title"
android:paddingTop="16dip"
android:paddingBottom="8dip"
android:paddingTop="16dp"
android:paddingBottom="8dp"
android:background="@drawable/bg_prefs_color"
android:paddingLeft="16dp"
android:layout_width="match_parent"

Loading…
Cancel
Save