From ab364237223625da1ce3553ad1cbfd9c69ccb51a Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 15 Aug 2020 20:43:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=BB=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/widget/prefs/NameListPreference.kt | 22 +- .../legado/app/ui/widget/prefs/Preference.kt | 63 +++--- .../app/ui/widget/prefs/SwitchPreference.kt | 8 +- app/src/main/res/values/attrs.xml | 189 +++++++++--------- app/src/main/res/xml/pref_config_read.xml | 45 +++-- 5 files changed, 197 insertions(+), 130 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt index ff3db4a9f..100f7f25e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/NameListPreference.kt @@ -6,19 +6,39 @@ import android.widget.TextView import androidx.preference.ListPreference import androidx.preference.PreferenceViewHolder import io.legado.app.R +import io.legado.app.lib.theme.bottomBackground +import io.legado.app.lib.theme.getPrimaryTextColor +import io.legado.app.utils.ColorUtils class NameListPreference(context: Context, attrs: AttributeSet) : ListPreference(context, attrs) { + private val isBottomBackground: Boolean + init { layoutResource = R.layout.view_preference widgetLayoutResource = R.layout.item_fillet_text + val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Preference) + isBottomBackground = typedArray.getBoolean(R.styleable.Preference_bottomBackground, false) + typedArray.recycle() } override fun onBindViewHolder(holder: PreferenceViewHolder?) { - val v = Preference.bindView(context, holder, icon, title, summary, widgetLayoutResource, R.id.text_view) + val v = Preference.bindView( + context, + holder, + icon, + title, + summary, + widgetLayoutResource, + R.id.text_view, + isBottomBackground = isBottomBackground + ) if (v is TextView) { v.text = entry + val bgColor = context.bottomBackground + val pTextColor = context.getPrimaryTextColor(ColorUtils.isColorLight(bgColor)) + v.setTextColor(pTextColor) } super.onBindViewHolder(holder) } diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt index 0e23d21d2..731e6118b 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/Preference.kt @@ -13,6 +13,10 @@ import androidx.core.view.isVisible import androidx.preference.PreferenceViewHolder import io.legado.app.R import io.legado.app.lib.theme.accentColor +import io.legado.app.lib.theme.bottomBackground +import io.legado.app.lib.theme.getPrimaryTextColor +import io.legado.app.lib.theme.getSecondaryTextColor +import io.legado.app.utils.ColorUtils import org.jetbrains.anko.layoutInflater import org.jetbrains.anko.sdk27.listeners.onLongClick import kotlin.math.roundToInt @@ -21,10 +25,13 @@ class Preference(context: Context, attrs: AttributeSet) : androidx.preference.Preference(context, attrs) { var onLongClick: (() -> Unit)? = null + private val isBottomBackground: Boolean init { - // isPersistent = true layoutResource = R.layout.view_preference + val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Preference) + isBottomBackground = typedArray.getBoolean(R.styleable.Preference_bottomBackground, false) + typedArray.recycle() } companion object { @@ -35,30 +42,33 @@ class Preference(context: Context, attrs: AttributeSet) : icon: Drawable?, title: CharSequence?, summary: CharSequence?, - weightLayoutRes: Int?, - viewId: Int?, + weightLayoutRes: Int? = null, + viewId: Int? = null, weightWidth: Int = 0, - weightHeight: Int = 0 + weightHeight: Int = 0, + isBottomBackground: Boolean = false ): T? { if (it == null) return null - val view = it.findViewById(R.id.preference_title) - if (view is TextView) { - view.text = title - view.isVisible = title != null && title.isNotEmpty() - - val tvSummary = it.findViewById(R.id.preference_desc) - if (tvSummary is TextView) { - tvSummary.text = summary - tvSummary.isGone = summary.isNullOrEmpty() - } - - val iconView = it.findViewById(R.id.preference_icon) - if (iconView is ImageView) { - iconView.isVisible = icon != null && icon.isVisible - iconView.setImageDrawable(icon) - iconView.setColorFilter(context.accentColor) - } - + val tvTitle = it.findViewById(R.id.preference_title) as TextView + tvTitle.text = title + tvTitle.isVisible = title != null && title.isNotEmpty() + val tvSummary = it.findViewById(R.id.preference_desc) as? TextView + tvSummary?.let { + tvSummary.text = summary + tvSummary.isGone = summary.isNullOrEmpty() + } + if (isBottomBackground && !tvTitle.isInEditMode) { + val bgColor = context.bottomBackground + val pTextColor = context.getPrimaryTextColor(ColorUtils.isColorLight(bgColor)) + tvTitle.setTextColor(pTextColor) + val sTextColor = context.getSecondaryTextColor(ColorUtils.isColorLight(bgColor)) + tvSummary?.setTextColor(sTextColor) + } + val iconView = it.findViewById(R.id.preference_icon) + if (iconView is ImageView) { + iconView.isVisible = icon != null && icon.isVisible + iconView.setImageDrawable(icon) + iconView.setColorFilter(context.accentColor) } if (weightLayoutRes != null && weightLayoutRes != 0 && viewId != null && viewId != 0) { @@ -98,7 +108,14 @@ class Preference(context: Context, attrs: AttributeSet) : } override fun onBindViewHolder(holder: PreferenceViewHolder?) { - bindView(context, holder, icon, title, summary, null, null) + bindView( + context, + holder, + icon, + title, + summary, + isBottomBackground = isBottomBackground + ) super.onBindViewHolder(holder) holder?.itemView?.onLongClick { onLongClick?.invoke() diff --git a/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt b/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt index 41038169f..9946f6c48 100644 --- a/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt +++ b/app/src/main/java/io/legado/app/ui/widget/prefs/SwitchPreference.kt @@ -12,8 +12,13 @@ import io.legado.app.lib.theme.accentColor class SwitchPreference(context: Context, attrs: AttributeSet) : SwitchPreferenceCompat(context, attrs) { + private val isBottomBackground: Boolean + init { layoutResource = R.layout.view_preference + val typedArray = context.obtainStyledAttributes(attrs, R.styleable.Preference) + isBottomBackground = typedArray.getBoolean(R.styleable.Preference_bottomBackground, false) + typedArray.recycle() } override fun onBindViewHolder(holder: PreferenceViewHolder?) { @@ -24,7 +29,8 @@ class SwitchPreference(context: Context, attrs: AttributeSet) : title, summary, widgetLayoutResource, - R.id.switchWidget + R.id.switchWidget, + isBottomBackground = isBottomBackground ) if (v is SwitchCompat && !v.isInEditMode) { ATH.setTint(v, context.accentColor) diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 9e920088b..243d21b37 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -4,67 +4,67 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + - + - - - - - + + + + + - - - - - - - - + + + + + + + + @@ -73,53 +73,53 @@ - - - - - - + + + + + + - - - + + + - - - + + + - - - - - - - + + + + + + + - - - - + + + + - + - - - - + + + + @@ -174,16 +174,25 @@ - - - - - - - - - - + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/pref_config_read.xml b/app/src/main/res/xml/pref_config_read.xml index 35a290bdd..1effb1567 100644 --- a/app/src/main/res/xml/pref_config_read.xml +++ b/app/src/main/res/xml/pref_config_read.xml @@ -8,7 +8,8 @@ android:title="@string/screen_direction" android:entries="@array/screen_direction_title" android:entryValues="@array/screen_direction_value" - app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> + app:iconSpaceReserved="false" + app:bottomBackground="true" /> \ No newline at end of file