recyclerView 中的图像不响应使用 glide 占位符将主题更改为暗模式
Images in recyclerView don't respond to change in theme to dark mode using glide placeholder
RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_all_songs"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
/>
Recycler 查看项目
<ImageView
android:id="@+id/img_song"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/app_logo"
app:albumId="@{song.albumId}"/>
<TextView
android:id="@+id/txt_song_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:ellipsize="end"
android:layout_marginStart="10dp"
android:maxLines="1"
android:text="@{song.title}"
android:textColor="@color/app_name_color"
android:textSize="15sp"
app:marquee="@{true}"
app:selected="@{safeUnbox(viewModel.currentData.id == song.id)}" />
绑定适配器
@BindingAdapter("app:albumId", "app:recycled", "app:blurred", requireAll = false)
fun setAlbumId(
view: ImageView,
albumId: Long,
recycled: Boolean = false,
blurred: Boolean = false)
{
view.clipToOutline = true
val uri = ContentUris.withAppendedId(ARTWORK_URI, albumId)
Glide
.with(view)
.load(uri)
.placeholder(R.drawable.ic_default_album_art)
.transform(CenterCrop(), RoundedCorners(8))
.into(view)
}
ic_default_album_art
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="30dp"
android:viewportWidth="30"
android:viewportHeight="30">
<path
android:pathData="M5,0L25,0A5,5 0,0 1,30 5L30,25A5,5 0,0 1,25 30L5,30A5,5 0,0 1,0
25L0,5A5,5 0,0 1,5 0z"
android:fillColor="@color/album_art_default_color"/>
</vector>
colors.xml
<color name="album_art_default_color">#EFEFEF</color>
colors.xml(晚上)
<color name="album_art_default_color">#4D4D4D</color>
更改主题代码
//on click change to light theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_light_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.light))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
builder.dismiss()
}
//on click change to dark theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_dark_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.dark))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
builder.dismiss()
}
//on click change to default theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_default_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.default_theme))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
builder.dismiss()
}
我的应用程序中有一个开关,可以切换为深色、浅色或默认(phone 主题)。
问题
当我切换到light/dark主题时;图像不改变颜色。它仅遵循 system/phone 主题。
因此,当phone默认主题为深色模式时,它使用深色资源,反之亦然。
应用 light/dark 模式资源或什么不支持 Glide 占位符??
也许你应该以这种方式加载占位符:
Glide
.with(view)
.load(uri)
.placeholder(ContextCompat.getDrawable(context, R.drawable.ic_default_album_art))
.transform(CenterCrop(), RoundedCorners(8))
.into(view)
RecyclerView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rcv_all_songs"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="10dp"
/>
Recycler 查看项目
<ImageView
android:id="@+id/img_song"
android:layout_width="30dp"
android:layout_height="30dp"
android:contentDescription="@string/app_logo"
app:albumId="@{song.albumId}"/>
<TextView
android:id="@+id/txt_song_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:ellipsize="end"
android:layout_marginStart="10dp"
android:maxLines="1"
android:text="@{song.title}"
android:textColor="@color/app_name_color"
android:textSize="15sp"
app:marquee="@{true}"
app:selected="@{safeUnbox(viewModel.currentData.id == song.id)}" />
绑定适配器
@BindingAdapter("app:albumId", "app:recycled", "app:blurred", requireAll = false)
fun setAlbumId(
view: ImageView,
albumId: Long,
recycled: Boolean = false,
blurred: Boolean = false)
{
view.clipToOutline = true
val uri = ContentUris.withAppendedId(ARTWORK_URI, albumId)
Glide
.with(view)
.load(uri)
.placeholder(R.drawable.ic_default_album_art)
.transform(CenterCrop(), RoundedCorners(8))
.into(view)
}
ic_default_album_art
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="30dp"
android:height="30dp"
android:viewportWidth="30"
android:viewportHeight="30">
<path
android:pathData="M5,0L25,0A5,5 0,0 1,30 5L30,25A5,5 0,0 1,25 30L5,30A5,5 0,0 1,0
25L0,5A5,5 0,0 1,5 0z"
android:fillColor="@color/album_art_default_color"/>
</vector>
colors.xml
<color name="album_art_default_color">#EFEFEF</color>
colors.xml(晚上)
<color name="album_art_default_color">#4D4D4D</color>
更改主题代码
//on click change to light theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_light_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.light))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
builder.dismiss()
}
//on click change to dark theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_dark_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.dark))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
builder.dismiss()
}
//on click change to default theme
sortView.findViewById<ConstraintLayout>(R.id.lyt_default_theme).setOnClickListener {
storageViewModel.changeTheme(resources.getString(R.string.default_theme))
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
builder.dismiss()
}
我的应用程序中有一个开关,可以切换为深色、浅色或默认(phone 主题)。
问题
当我切换到light/dark主题时;图像不改变颜色。它仅遵循 system/phone 主题。
因此,当phone默认主题为深色模式时,它使用深色资源,反之亦然。
应用 light/dark 模式资源或什么不支持 Glide 占位符??
也许你应该以这种方式加载占位符:
Glide
.with(view)
.load(uri)
.placeholder(ContextCompat.getDrawable(context, R.drawable.ic_default_album_art))
.transform(CenterCrop(), RoundedCorners(8))
.into(view)