【问题标题】:Change size of cursor in TextField (Jetpack Compose)更改 TextField 中光标的大小(Jetpack Compose)
【发布时间】:2021-06-16 10:14:14
【问题描述】:

我想减少文本字段中光标的“高度”。我该怎么做?

我的默认光标高度的 TextField 如下所示:

【问题讨论】:

  • 尝试减少fontSize,你也可以修改高度和样式

标签: android textfield android-jetpack-compose


【解决方案1】:

我们可以通过CoreTextField.cursorBrush自定义文本字段光标,带有Brush.verticalGradientcolorStops参数。

BasicTextField(
    cursorBrush = Brush.verticalGradient(
        0.00f to Color.Transparent,
        0.35f to Color.Transparent,
        0.35f to Color.Green,
        0.90f to Color.Green,
        0.90f to Color.Transparent,
        1.00f to Color.Transparent
    )
)

【讨论】:

    【解决方案2】:

    res/drawable/custom_cursor.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
       
     <size
            android:width="2dp"// cursor width
            android:height="28dp" />// cursor height should be noted that the height can not be set edittext adaptation, otherwise, the cursor height or size of the input character
        
    <solid android:color="@color/azure" />// color of the cursor
        
    <padding    // This parameter allows the cursor is highly inconsistent with the text size, text size I need is here 14dp
            android:bottom="7dp"
            android:top="7dp" />
    </shape>
    

    在您的编辑文本中:

    android:textCursorDrawable="@drawable/custom_cursor"
    

    【讨论】:

    • 哥们,我用的是Jetpack compose,不是视图系统。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2021-10-14
    • 2021-11-14
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多