android Jetpack Composer无法将搜索栏背景色设置为白色

gorkyyrv  于 2023-05-15  发布在  Android
关注(0)|答案(3)|浏览(161)

我有一个搜索栏与textfield当我试图设置背景颜色为白色它带有灰色但我可以使它成为其他颜色只有白色不工作如果我改变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)
        )
    }
}
}

它看起来像什么:

背景和条的颜色都是白色,但看起来不同

ibps3vxo

ibps3vxo1#

删除背景修饰符:

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

并用途:

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

jogvjijk

jogvjijk2#

在文本字段中添加此行:

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

cczfrluj3#

在compose的最新版本中,“backgroundColor”属性更改为“containerColor”
用途:

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

相关问题