你如何为 androidx 首选项图标着色?
How do you tint an androidx Preference icon?
我从库中获得了一个可绘制的矢量,它定义了白色前景和白色色调:
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector>
我正在尝试将此可绘制对象用作 androidx.preference
元素上的图标,但 tint
似乎不是 Preference
的 属性 class,设置它没有任何作用。图标保持白色。
<Preference
app:icon="@drawable/ic_baseline_email_24"
app:iconSpaceReserved="true"
app:key="email"
app:summary="@string/supportEmail"
app:tint="@color/primary"
app:title="Report an issue">
<intent
android:action="android.intent.action.SENDTO"
android:data="@string/supportEmail" />
</Preference>
有什么好方法可以让我根据偏好对图标进行着色?
https://github.com/Gericop/Android-Support-Preference-V7-Fix/issues/129#issuecomment-385314102
我找到了一个更好的方案,只需要在style上配置,创建values-night->color
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="preferenceTheme">@style/PreferenceTheme</item>
</style>
<style name="PreferenceTheme" parent="@style/PreferenceThemeOverlay.v14.Material">
<item name="android:tint">@color/pref_icon_color</item>
</style>
并添加
override fun onCreate(savedInstanceState: Bundle?) {
delegate.setLocalNightMode(if (ThemeOverlayManager.isDarkTheme()) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
super.onCreate(savedInstanceState);
}
使用android:tint
,app:tint
无效。
这是一个不会导致工具栏色调出现问题的解决方案。在 PreferenceFragmentCompat 的 onViewCreated() 中调用它。
val rv = view.findViewById<RecyclerView>(androidx.preference.R.id.recycler_view)
rv?.viewTreeObserver?.addOnDrawListener {
rv.children.forEach { pref ->
val icon = pref.findViewById<View>(android.R.id.icon) as? PreferenceImageView
icon?.let {
if (it.tag != "painted") {
it.setColorFilter(
ContextCompat.getColor(requireContext(), R.color.iconColor),
PorterDuff.Mode.SRC_IN
)
it.tag = "painted"
}
}
}
}
我从库中获得了一个可绘制的矢量,它定义了白色前景和白色色调:
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,4L4,4c-1.1,0 -1.99,0.9 -1.99,2L2,18c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2zM20,8l-8,5 -8,-5L4,6l8,5 8,-5v2z"/>
</vector>
我正在尝试将此可绘制对象用作 androidx.preference
元素上的图标,但 tint
似乎不是 Preference
的 属性 class,设置它没有任何作用。图标保持白色。
<Preference
app:icon="@drawable/ic_baseline_email_24"
app:iconSpaceReserved="true"
app:key="email"
app:summary="@string/supportEmail"
app:tint="@color/primary"
app:title="Report an issue">
<intent
android:action="android.intent.action.SENDTO"
android:data="@string/supportEmail" />
</Preference>
有什么好方法可以让我根据偏好对图标进行着色?
https://github.com/Gericop/Android-Support-Preference-V7-Fix/issues/129#issuecomment-385314102
我找到了一个更好的方案,只需要在style上配置,创建values-night->color
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="preferenceTheme">@style/PreferenceTheme</item>
</style>
<style name="PreferenceTheme" parent="@style/PreferenceThemeOverlay.v14.Material">
<item name="android:tint">@color/pref_icon_color</item>
</style>
并添加
override fun onCreate(savedInstanceState: Bundle?) {
delegate.setLocalNightMode(if (ThemeOverlayManager.isDarkTheme()) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
super.onCreate(savedInstanceState);
}
使用android:tint
,app:tint
无效。
这是一个不会导致工具栏色调出现问题的解决方案。在 PreferenceFragmentCompat 的 onViewCreated() 中调用它。
val rv = view.findViewById<RecyclerView>(androidx.preference.R.id.recycler_view)
rv?.viewTreeObserver?.addOnDrawListener {
rv.children.forEach { pref ->
val icon = pref.findViewById<View>(android.R.id.icon) as? PreferenceImageView
icon?.let {
if (it.tag != "painted") {
it.setColorFilter(
ContextCompat.getColor(requireContext(), R.color.iconColor),
PorterDuff.Mode.SRC_IN
)
it.tag = "painted"
}
}
}
}