【问题标题】:How to add drawable's next to textfield in jetpack compose?如何在jetpack compose中的文本字段旁边添加drawable?
【发布时间】:2022-06-29 23:49:05
【问题描述】:

我试图弄清楚如何将可绘制对象添加到文本字段 (OutlinedTextField) 内的文本中,但没有任何成功。我已经在drawable文件夹中创建了一个drawable。我需要创建一个单独的可组合来插入它还是可以直接将它添加到文本字段中?

这是我的可组合文本字段:


        Spacer(modifier = Modifier.height(20.dp))
        OutlinedTextField(
            value = username.value,
            onValueChange = { newText ->
                username.value = newText
            },
            label = { Text(text = "Username") },
            placeholder = { Text("") }, // placeholder / hint
            modifier = Modifier
                .fillMaxWidth(180F)
        )

【问题讨论】:

  • 使用leadingIcon 代替占位符属性
  • 您是在谈论领先的图标还是只是文本中的图像?
  • 只是一个前导图标,类似于文本旁边的电子邮件图标。 gyazo.com/0efdcbf72fa066784c8f5991e013fc5a

标签: android kotlin android-jetpack-compose android-drawable


【解决方案1】:

使用leadingIcon 代替placeHolder

           OutlinedTextField(
               value = username.value,
               onValueChange = { newText ->
                   username.value = newText
               },
               label = { Text(text = "Username") },
               leadingIcon = { Icon(imageVector = Icons.Default.Place, contentDescription = null)},
               modifier = Modifier
                   .fillMaxWidth(180F)
           )

【讨论】:

  • 这解决了,谢谢!
猜你喜欢
  • 2023-01-31
  • 2019-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 2022-08-08
  • 2022-06-27
相关资源
最近更新 更多