我有以下可组合函数来构建芯片:
@Composable
fun CategoryChip(
category: String,
isSelected: Boolean = false,
onSelectedCategoryChanged: (String) -> Unit,
onExecuteSearch: () -> Unit
) {
Surface(
modifier = Modifier.padding(end = 8.dp, bottom = 8.dp),
elevation = 8.dp,
shape = RoundedCornerShape(16.dp),
color = when {
isSelected -> colorResource(R.color.teal_200)
else -> colorResource(R.color.purple_500)
}
) {
Row(modifier = Modifier
.toggleable(
value = isSelected,
onValueChange = {
onSelectedCategoryChanged(category)
onExecuteSearch()
}
)) {
Text(
text = category,
style = MaterialTheme.typography.body2,
color = Color.White,
modifier = Modifier.padding(8.dp)
)
}
}
} …
Run Code Online (Sandbox Code Playgroud) android android-chips android-jetpack-compose android-jetpack-compose-material3
我试图将按钮的图标向左对齐并保持文本居中。有什么想法可以实现这一点吗?
我的可组合项:
@Composable
fun CustomButton() {
MaterialTheme {
OutlinedButton(
onClick = {},
modifier = Modifier
.padding(12.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = Color.White),
shape = RoundedCornerShape(4.dp)) {
Icon(
imageVector = Icons.Default.FavoriteBorder,
contentDescription = null,
modifier = Modifier.padding(start = 4.dp)
)
Text(text = "Like", color = Color.Grey)
}
}
}
Run Code Online (Sandbox Code Playgroud)
android android-jetpack-compose android-compose-button android-jetpack-compose-button
如何更改 IconButton 的波纹颜色?
我尝试这样做,但它没有改变:
IconButton(
onClick = { onClick() },
modifier = Modifier.clickable(
onClick = { onClick() },
indication = rememberRipple(color = MyCustomTheme.colors.primary),
interactionSource = remember { MutableInteractionSource() },
)
)
Run Code Online (Sandbox Code Playgroud) android android-jetpack-compose android-jetpack-compose-button
运行测试时出现以下错误:
无法解析配置 ':app:debugUnitTestCompileClasspath' 的所有文件。找不到 android.arch.core:core-testing:2.1.0。
如果有任何帮助,我正在使用 Android Studio Arctic Fox | 2020.3.1 金丝雀 6
完整的堆栈跟踪(截图,否则太长了):
依赖项:
implementation Dependencies.kotlin_standard_library
implementation Dependencies.ktx
implementation Dependencies.appcompat
implementation Dependencies.constraint_layout
implementation Dependencies.material
implementation Dependencies.lifecycle_livedata
implementation Dependencies.lifecycle_runtime
implementation Dependencies.lifecycle_common_java8
implementation Dependencies.lifecycle_viewmodel
// Hilt
implementation Dependencies.hilt_android
kapt AnnotationProcessing.hilt_android_compiler
implementation Dependencies.hilt_lifecycle_viewmodel
kapt AnnotationProcessing.hilt_compiler
// Coroutines
implementation Dependencies.coroutines
// Retrofit
implementation Dependencies.retrofit
implementation Dependencies.converter_gson
implementation Dependencies.logging_interceptor
// Glide
implementation Dependencies.glide
kapt AnnotationProcessing.glide_compiler
// Navigation
implementation Dependencies.navigation_fragment
implementation Dependencies.navigation_ktx
// PhotoView - Zooming ImageView
implementation Dependencies.photo_view
// Room
implementation Dependencies.room_runtime
kapt …
Run Code Online (Sandbox Code Playgroud) 我在适配器和自定义视图中使用以下扩展函数进行视图绑定。
inline fun <T : ViewBinding> ViewGroup.viewBinding(crossinline bindingInflater: (LayoutInflater, ViewGroup, Boolean) -> T) =
bindingInflater.invoke(LayoutInflater.from(this.context), this, true)
Run Code Online (Sandbox Code Playgroud)
val binding = viewBinding(ComponentButtonBinding::inflate)
Run Code Online (Sandbox Code Playgroud)
对于自定义视图,它工作得很好,但对于适配器,我需要调用中的布尔值为 false。我怎样才能解决这个问题并使这个功能对两者都起作用?
我有以下数据类类别:
data class Category(
val id: Int = DEFAULT_ID,
val name: String = "",
val picture: String = "",
val subcategories: List<Category> = listOf()
)
Run Code Online (Sandbox Code Playgroud)
我正在构建一个函数,该函数接受 id 列表,并且需要在类别列表中搜索(如果该列表包含这些 id)。如果它包含id,我需要将类别保存在categoryListResult中。
这就是我编写的方式,并且它有效,但我不确定这是否是 Kotlin 中最有效的方法。
private fun getPopCategories(listOfIds : MutableList<Int>) {
val categoryListResult = mutableListOf<Category>()
getCategories.execute(
onSuccess = { categories -> categories.forEach{ category ->
if (listOfIds.contains(category.id)) categoryListResult.add(category)
if (category.hasSubcategories()) {
category.subcategories.forEach { subcategory ->
if (listOfIds.contains(subcategory.id)) categoryListResult.add(subcategory)
}
}
}
}
)
}
Run Code Online (Sandbox Code Playgroud)