Ely*_*lye 2 android imageview android-selector android-jetpack-compose
我有一个图像视图,它使用选择器
<ImageView
android:id="@+id/image_view"
android:src="@drawable/ic_back_hand_24_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
选择器根据状态在 2 个矢量可绘制图像之间进行选择
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ic_baseline_back_hand_24" android:state_selected="true" />
<item android:drawable="@drawable/ic_outline_back_hand_24" />
</selector>
Run Code Online (Sandbox Code Playgroud)
这非常方便,我们可以通过以编程方式将状态设置为 True 或 False 来交换图像
theImageView.setOnClickListener {
this.isSelected = !this.isSelected
}
Run Code Online (Sandbox Code Playgroud)
但是,如果我将其放在 Jetpack Compose 上,Icon如下所示
Icon(painterResource(id = R.drawable.ic_baseline_back_hand_24), contentDescription = "")
Run Code Online (Sandbox Code Playgroud)
它崩溃说明
java.lang.IllegalArgumentException: Only VectorDrawables and rasterized asset types are supported ex. PNG, JPG
Run Code Online (Sandbox Code Playgroud)
Jetpack Compose Icon 没有办法加载选择器 XML 吗?
您可以使用不同的东西来更改组件在不同状态下的显示方式。
例如:
var selected by remember { mutableStateOf(false) }
val drawableResource = if (selected) R.drawable.xxx else R.drawable.xxx
Image(
painter = painterResource(id = drawableResource),
contentDescription = "contentDescription",
modifier = Modifier.clickable { selected = !selected }
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2261 次 |
| 最近记录: |