简体   繁体   English

jetpack compose 无法将搜索栏背景颜色设置为白色

[英]jetpack compose can't set search bar background color to white

I have a search bar with textfield when I try to set background color to white it comes with gray but I can make it to other colors only white not working if I change Textfiled to BasicTexfield it works fine but can't set the Icon top start当我尝试将背景颜色设置为白色时,我有一个带有文本字段的搜索栏,它带有灰色,但是如果我将 Textfiled 更改为 BasicTexfield,我可以将其设置为其他颜色,只有白色不起作用,它工作正常,但无法设置图标顶部开始

@Composable
fun DoctorListScreen(
navController: NavController,
viewModel: DoctorListViewModel = hiltViewModel()
) {
Surface(
    color = Color.White,
    modifier = Modifier.fillMaxSize(1f)
) {
    Column {
        Spacer(modifier = Modifier.padding(top = 15.dp))
        SearchBar(
            hint = "Klinik ara..", modifier = Modifier
                .fillMaxWidth()
                .padding(15.dp)
        ) {
        }
        CheckGender(modifier = Modifier.padding(15.dp))

    }
}
}


@Composable
fun SearchBar(
modifier: Modifier = Modifier,
hint: String = "",
onSearch: (String) -> Unit = {},
) {

var text by remember {
    mutableStateOf("")
}

var isHintDisplayed by remember {
    mutableStateOf(hint != "")
}
Box(modifier = modifier) {
    TextField(value = text, onValueChange = {
        text = it
        onSearch(it)
    }, leadingIcon = {
        Icon(painter = painterResource(id = R.drawable.search), contentDescription = null)
    }, maxLines = 1,
        singleLine = true,
        modifier = Modifier
            .fillMaxWidth()
            .shadow(5.dp, shape = RoundedCornerShape(10.dp))
            .background(Color.White, shape = RoundedCornerShape(10.dp))
            .onFocusChanged {
                isHintDisplayed = it.isFocused != true && text.isEmpty()
            })
    if (isHintDisplayed) {
        Text(
            text = hint,
            color = Color.LightGray,
            modifier = Modifier.padding(horizontal = 50.dp, vertical = 16.dp)
        )
    }
}
}

how it looks like :它的样子:

在此处输入图像描述

both background and bar color is white but seems different背景和条形颜色都是白色,但看起来不同

Add this line in your Textfield:在您的文本字段中添加这一行:

colors = TextFieldDefaults.textFieldColors(
    backgroundColor = Color.White)

Remove the background modifier:移除背景修饰符:

//background(Color.White, shape = RoundedCornerShape(10.dp))

and use:并使用:

 TextField(
         /* .... */
        shape = RoundedCornerShape(10.dp),
        colors = TextFieldDefaults.textFieldColors(backgroundColor = Color.White),
 )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Jetpack Compose 上将背景设为白色 - Can´t put the background in white on jetpack compose SystemUIController 不会设置状态栏颜色 - Jetpack Compose Accompanist - SystemUIController won't set status bar color - Jetpack Compose Accompanist 在 Jetpack Compose 中设置状态栏颜色渐变匹配内容背景渐变 - Set status bar color gradient matching the content background gradient in Jetpack Compose Jetpack Compose 中按钮的背景颜色 - background color on Button in Jetpack Compose 为整个应用程序设置背景颜色。 Android,Jetpack Compose - Set background color for whole app. Android, Jetpack Compose Jetpack compose - 更改底部栏切口颜色 - Jetpack compose - change bottom bar cutout color 无法设置主题为“ Holo Dark”的操作栏的背景颜色 - Can't set the background color of Action Bar with theme Holo Dark Android Jetpack Compose - 背景颜色不会改变 - Android Jetpack Compose - Background color does not change Jetpack Compose Spacer,背景颜色未显示在屏幕上 - Jetpack Compose Spacer with background color not shown on screen Android Jetpack compose 如何测试背景颜色 - Android Jetpack compose how to test background color
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM