Android compose OutlinedTextField 输入框设置固定高度后,内容挤压显示不全

server/2024/9/23 0:57:10/

原因:

decorationBox里边contentPadding() 默认为16.dp
internal val TextFieldPadding = 16.dp

修改方法1:

copy OutlinedTextField.kt 源码,decorationBox的contentPadding参数开放出来

/*** @author   创建人:蒙石瑞* @date     创建时间:2024/8/7 09:54* @Description    创建内容:(这里用一句话描述这个类的作用)*/internal val OutlinedTextFieldTopPadding = 8.dp@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CustomOutlinedTextField(value: String,onValueChange: (String) -> Unit,modifier: Modifier = Modifier,enabled: Boolean = true,readOnly: Boolean = false,textStyle: TextStyle = LocalTextStyle.current,label: @Composable (() -> Unit)? = null,placeholder: @Composable (() -> Unit)? = null,leadingIcon: @Composable (() -> Unit)? = null,trailingIcon: @Composable (() -> Unit)? = null,prefix: @Composable (() -> Unit)? = null,suffix: @Composable (() -> Unit)? = null,supportingText: @Composable (() -> Unit)? = null,isError: Boolean = false,visualTransformation: VisualTransformation = VisualTransformation.None,keyboardOptions: KeyboardOptions = KeyboardOptions.Default,keyboardActions: KeyboardActions = KeyboardActions.Default,singleLine: Boolean = false,maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,minLines: Int = 1,interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },shape: Shape = OutlinedTextFieldDefaults.shape,colors: TextFieldColors = OutlinedTextFieldDefaults.colors(),//设置内容边距contentPadding: PaddingValues = contentPadding(),
) {// If color is not provided via the text style, use content color as a defaultval textColor = textStyle.color.takeOrElse {textColor(enabled, isError, interactionSource).value}val mergedTextStyle = textStyle.merge(TextStyle(color = textColor))CompositionLocalProvider(LocalTextSelectionColors provides LocalTextSelectionColors.current) {BasicTextField(value = value,modifier = if (label != null) {modifier// Merge semantics at the beginning of the modifier chain to ensure padding is// considered part of the text field..semantics(mergeDescendants = true) {}.padding(top = OutlinedTextFieldTopPadding)} else {modifier}.defaultErrorSemantics(isError, "error").defaultMinSize(minWidth = OutlinedTextFieldDefaults.MinWidth,minHeight = OutlinedTextFieldDefaults.MinHeight),onValueChange = onValueChange,enabled = enabled,readOnly = readOnly,textStyle = mergedTextStyle,cursorBrush = SolidColor(cursorColor(isError).value),visualTransformation = visualTransformation,keyboardOptions = keyboardOptions,keyboardActions = keyboardActions,interactionSource = interactionSource,singleLine = singleLine,maxLines = maxLines,minLines = minLines,decorationBox = @Composable { innerTextField ->OutlinedTextFieldDefaults.DecorationBox(value = value,visualTransformation = visualTransformation,innerTextField = innerTextField,placeholder = placeholder,label = label,leadingIcon = leadingIcon,trailingIcon = trailingIcon,prefix = prefix,suffix = suffix,supportingText = supportingText,singleLine = singleLine,enabled = enabled,isError = isError,interactionSource = interactionSource,colors = colors,contentPadding = contentPadding,container = {OutlinedTextFieldDefaults.ContainerBox(enabled,isError,interactionSource,colors,shape)})})}
}internal fun Modifier.defaultErrorSemantics(isError: Boolean,defaultErrorMessage: String,
): Modifier = if (isError) semantics { error(defaultErrorMessage) } else this@Composable
internal fun cursorColor(isError: Boolean): State<Color> {return rememberUpdatedState(if (isError) Color(red = 179,green = 38,blue = 30) else Color(red = 103, green = 80, blue = 164))
}@Composable
fun textColor(enabled: Boolean,isError: Boolean,interactionSource: InteractionSource
): State<Color> {val focused by interactionSource.collectIsFocusedAsState()val targetValue = when {!enabled -> Color(red = 28, green = 27, blue = 31)isError -> Color(red = 28, green = 27, blue = 31)focused -> Color(red = 28, green = 27, blue = 31)else -> Color(red = 28, green = 27, blue = 31)}return rememberUpdatedState(targetValue)
}

方法二:基于

BasicTextField 自定义
OutlinedTextFieldDefaults.DecorationBox
//            BasicTextField(
//                value = text,
//                onValueChange = { text = it },
//                modifier = Modifier
//                    .height(36.dp),
//                singleLine = true,
//            ) { innerTextField ->
//                OutlinedTextFieldDefaults.DecorationBox(
//                    value = text,
//                    innerTextField = innerTextField,
//                    enabled = true,
//                    singleLine = true,
//                    label = { Text("工号", modifier = Modifier.padding(0.dp)) },
//                    visualTransformation = VisualTransformation.None,
//                    interactionSource = interactionSource,
//                    contentPadding = TextFieldDefaults.textFieldWithoutLabelPadding(
//                        top = 0.dp,
//                        bottom = 0.dp
//                    )
//                )
//            }

使用


http://www.ppmy.cn/server/98966.html

相关文章

遗嘱符合法定形式要件,遗嘱继承人有权继承房产

因病重住院时儿子不闻不问&#xff0c;女子无奈立下遗嘱&#xff0c;约定由弟弟照料其余生&#xff0c;去世后将房产留给弟弟。女子去世后&#xff0c;女子的弟弟和儿子为遗产分割起了争执&#xff0c;闹上法庭。近日&#xff0c;江苏省南通市中级人民法院对这起遗嘱继承纠纷案…

学术周交流与学习节选

文章目录 1、粒度多模态运动分析1.1 免特征重建的终身行人重识别1.2 无样本保留的终身行人重识别1.3 粒度多模态运动之类增量学习1.4 粒度多模态之人体姿态估计扩散模型 2、深度伪造的被动取证与主动防御2.1 研究现状及主要方法2.2 基于梯度的伪影特征表示2.3 基于伪造自适应学…

GIS赋能数字经济的地理脉络

在全球数字化转型的洪流中&#xff0c;数字经济以其惊人的速度与规模&#xff0c;重塑全球经济格局&#xff0c;成为推动社会进步的关键力量。地理信息系统&#xff08;GIS&#xff09;在数字经济的浪潮中扮演着不可替代的角色&#xff0c;它不仅是数字空间信息的集大动脉&…

Go Web 项目使用 Gin 框架:Hello World 示例

Go 是一种静态类型的编程语言&#xff0c;以其简洁、高效和并发性能而闻名。Gin 是一个由 Go 语言编写的 Web 框架&#xff0c;它提供了一种快速且易于使用的方式来构建 Web 应用程序。本文将指导您如何新建一个 Go Web 项目&#xff0c;并使用 Gin 框架启动一个简单的 “Hello…

基于Martin实现MapboxGL自定义底图

概述 本文分享基于Martin实现MapboxGL底图的自定义。 实现后效果 Martin简介 Martin 是一个瓦片服务器&#xff0c;它能够从 PostGIS 数据库、PMTiles&#xff08;本地或远程&#xff09;以及 [MBTiles] (https://github.com/mapbox/mbtiles-spec) 文件中快速生成并提供矢量瓦…

2024世界机器人大会将于8月21日至25日在京举行

2024年的世界机器人大会预定于8月21日至25日&#xff0c;在北京经济技术开发区的北人亦创国际会展中心隆重举办。 本届大会以“共育新质生产力 共享智能新未来”为核心主题&#xff0c;将汇聚来自全球超过300位的机器人行业专家、国际组织代表、杰出科学家以及企业家&#xff0…

Go语言本机多版本管理

go1.19.6 && go1.22.4 背景 当前安装的版本是1.22.4&#xff0c;但是对于某些项目有低版本的要求&#xff0c;所以说需要安装低版本 最终使用别名调用的方法 方法 安装go1.19.6压缩包 https://go.dev/dl/ 我下载该go1.19.6.darwin-arm64.tar.gz包 解压缩到指定…

Linux IPC-System V

前言 我们上一期介绍了&#xff0c;用管道实现进程间通信&#xff0c;以及介绍了管道的原理&#xff01;本期我们来介绍System V系列的进程间通信&#xff01;本博客主要介绍共享内存&#xff0c;对消息队列和信号量只介绍原理和接口调用&#xff01; 本期内容介绍 一、Syst…