是否有办法动态地在杰普特包装材料中确定icon值?
例,而不是:
Icon(Icons.Filled.Print, "print")
我谨:
Icon(Icons.Filled.(iconValue), iconValueName)
是否有办法动态地在杰普特包装材料中确定icon值?
例,而不是:
Icon(Icons.Filled.Print, "print")
我谨:
Icon(Icons.Filled.(iconValue), iconValueName)
你们可以利用 Java的思考。 我依靠的是,每个材料一栏都放在单独档案中,所有材料均在<条码>上申报。
@Composable
fun IconByName(name: String) {
val icon: ImageVector? = remember(name) {
try {
val cl = Class.forName("androidx.compose.material.icons.filled.${name}Kt")
val method = cl.declaredMethods.first()
method.invoke(null, Icons.Filled) as ImageVector
} catch (_: Throwable) {
null
}
}
if (icon != null) {
Icon(icon, "$name icon")
}
}
http://stackoverflow.com/a/48641786/3585796”
我也将用两种微调来对这一逻辑的测试,例如,对未来释放中的一些变化进行对比——一揽子名称或将多个微调合在同一档案中,尽管这不大可能。
根据“哲学-dukhov”的答复,本文概括介绍了执行情况:
// File MaterialIconUtils.kt
package your.utils.package.goes.here
import android.util.Log
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.BugReport
import androidx.compose.ui.graphics.vector.ImageVector
/** Regex to split icon name. */
private val splitter = Regex("\.")
/** Prefix for Material icons package. */
private val iconsPackage = "androidx.compose.material."
/** Decodes a Material Icon using its "compose" name.
*
* For example:
* - Icon `Icons.AutoMirrored.TwoTone.ArrowBack`
* - Looks for class `androidx.compose.material.icons.filled.ArrowBackKt`
* - Attempts to find method `getArrowBack(androidx.compose.material.icons.Icons$AutoMirrored$TwoTone)`
* - Executes such method using static instance `Icons.AutoMirrored.TwoTone`
*
* Remember that Material Icons are organized as follows:
* ```
* Icons.Default.<iconName> --> androidx.compose.material.icons.filled
* Icons.Filled.<iconName> --> androidx.compose.material.icons.filled
* Icons.Rounded.<iconName> --> androidx.compose.material.icons.rounded
* Icons.Outlined.<iconName> --> androidx.compose.material.icons.outlined
* Icons.TwoTone.<iconName> --> androidx.compose.material.icons.twotone
* Icons.Sharp.<iconName> --> androidx.compose.material.icons.sharp
* Icons.AutoMirrored.Default.<iconName> --> androidx.compose.material.icons.automirrored.filled
* Icons.AutoMirrored.Filled.<iconName> --> androidx.compose.material.icons.automirrored.filled
* Icons.AutoMirrored.Rounded.<iconName> --> androidx.compose.material.icons.automirrored.rounded
* Icons.AutoMirrored.Outlined.<iconName> --> androidx.compose.material.icons.automirrored.outlined
* Icons.AutoMirrored.Rounded.<iconName> --> androidx.compose.material.icons.automirrored.rounded
* Icons.AutoMirrored.TwoTone.<iconName> --> androidx.compose.material.icons.automirrored.twotone
* Icons.AutoMirrored.Sharp.<iconName> --> androidx.compose.material.icons.automirrored.sharp
* ```
*
* Also, don t forget to include in gradle:
* ```
* platform("androidx.compose:compose-bom:2024.02.01").also { compose ->
* implementation(compose)
* androidTestImplementation(compose)
* :
* implementation("androidx.compose.material:material-icons-core")
* implementation("androidx.compose.material:material-icons-extended")
* :
* }
* ```
*
* @param name Icon name.
* @param default Default icon if [name] is invalid.
*/
fun decodeMaterialIcon(
name: String,
default: ImageVector? = Icons.Default.BugReport
): ImageVector? =
try {
val parts = splitter.split(name)
val className: String
val typeName: String
val iconName: String
when (parts.size) {
3 -> {
className = buildString {
append(iconsPackage)
append(parts[0].lowercase())
append( . )
val type = parts[1].lowercase()
if (type == "default") {
append("filled")
} else {
append(type)
}
append( . )
append(parts[2])
append("Kt")
}
typeName = buildString {
append(parts[0])
append( . )
val type = parts[1]
if (type == "Default") {
append("Filled")
} else {
append(type)
}
}
iconName = parts[2]
}
4 -> {
className = buildString {
append(iconsPackage)
append(parts[0].lowercase())
append( . )
append(parts[1].lowercase())
append( . )
val type = parts[2].lowercase()
if (type == "default") {
append("filled")
} else {
append(type)
}
append( . )
append(parts[3])
append("Kt")
}
typeName = buildString {
append(parts[0])
append( . )
append(parts[1])
append( . )
val type = parts[2]
if (type == "Default") {
append("Filled")
} else {
append(type)
}
}
iconName = parts[3]
}
else -> throw IllegalArgumentException("Invalid icon-name $name ")
}
val typeClass: Any = when (typeName) {
"Icons.Filled" -> Icons.Filled
"Icons.Outlined" -> Icons.Outlined
"Icons.Rounded" -> Icons.Rounded
"Icons.TwoTone" -> Icons.TwoTone
"Icons.Sharp" -> Icons.Sharp
"Icons.AutoMirrored.Filled" -> Icons.AutoMirrored.Filled
"Icons.AutoMirrored.Outlined" -> Icons.AutoMirrored.Outlined
"Icons.AutoMirrored.Rounded" -> Icons.AutoMirrored.Rounded
"Icons.AutoMirrored.TwoTone" -> Icons.AutoMirrored.TwoTone
"Icons.AutoMirrored.Sharp" -> Icons.AutoMirrored.Sharp
else -> throw IllegalArgumentException("Invalid icon-name $name ")
}
Class.forName(className).getDeclaredMethod("get$iconName", typeClass.javaClass).invoke(
null,
typeClass
) as ImageVector
} catch (e: Throwable) {
Log.d("icons", "Icon-error: ${e.message}")
/* Note: if anything goes wrong:
* - re-throw exception, or
* - return a default icon (e.g. BugReport)
* Warning: this may introduce unexpected results, or
* - return null - composables should not render icon, or use a different mechanism.
*/
default
}
为使堆肥更容易使用,将签名改为:
指出为了方便堆肥和避免作废,改签名是:
fun decodeMaterialIcon(
name: String,
default: ImageVector = Icons.Default.BugReport
): ImageVector = ...
页: 1
/**
* Utility delegate to construct a Material icon with default size information.
* This is used by generated icons, and should not be used manually.
*
* @param name the full name of the generated icon
* @param autoMirror determines if the vector asset should automatically be mirrored for right to
* left locales
* @param block builder lambda to add paths to this vector asset
*/
inline fun materialIcon(
name: String,
autoMirror: Boolean = false,
block: ImageVector.Builder.() -> ImageVector.Builder
): ImageVector = ImageVector.Builder(
name = name,
defaultWidth = MaterialIconDimension.dp,
defaultHeight = MaterialIconDimension.dp,
viewportWidth = MaterialIconDimension,
viewportHeight = MaterialIconDimension,
autoMirror = autoMirror
).block().build()
然后,你可以在发言时使用,选择适当的图像媒介。
when(imageStringFromWeb) {
is "email" -> {
Icon(Icons.Filled.Email, null)
}
...
}
I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...
I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...
I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...
I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...
To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?
When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...
I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...
I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...