【问题标题】:How to add stroke in content of textview in android如何在android中的textview内容中添加笔画
【发布时间】:2018-01-09 07:25:18
【问题描述】:

我不想使用图像资源。 textview 中的文本应如上图所示。有没有办法在 xml 或 java/kotlin 代码中执行此样式。我也不想使用帆布绘画。请帮忙。

【问题讨论】:

  • 使用字体而不是创建xml。
  • @HemantParmar 我使用了外部字体,但是如何更改笔画的颜色?
  • 只需更改文本颜色

标签: android text textview stroke


【解决方案1】:

在 Internet 上的某个地方找到。我创建了一个自定义的textview,在onDraw方法中使用了下面的代码,它使用画布绘制来创建textview效果的Text Outline。

 override fun onDraw(canvas: Canvas?) {
    super.onDraw(canvas)
    if(strokeColor!=null)
    {
        val textColor = textColors

        val paint = this.paint

        paint.style = Paint.Style.STROKE
        paint.strokeJoin = strokeJoin
        paint.strokeMiter = strokeMiter    //10f
        this.setTextColor(strokeColor)
        paint.strokeWidth = strokeWidth    //15f

        super.onDraw(canvas)
        paint.style = Paint.Style.FILL

        setTextColor(textColor)
        super.onDraw(canvas)

    }
}

【讨论】:

  • 感谢分享您的解决方案
  • 删除这个解决方案或者修复它,它将永远循环onDraw方法,递归
【解决方案2】:

看看这个library

示例

<com.biomorgoth.outlinetextview.StrokedTextView
        android:id="@+id/your_text_view"
        android:text="I am a StrokedTextView!"
        android:textColor="@android:color/white"
        android:textSize="25sp"
        strokeAttrs:textStrokeColor="@android:color/black"
        strokeAttrs:textStrokeWidth="1.7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

示例 2

<com.biomorgoth.outlinetextview.StrokedEditText
        android:id="@+id/your_edit_text"
        android:hint="Write here!"
        android:textColor="@android:color/white"
        android:textColorHint="#5fff"
        android:textSize="25sp"
        strokeAttrs:textStrokeColor="@android:color/black"
        strokeAttrs:textStrokeWidth="1.7"
        strokeAttrs:textHintStrokeColor="#5000"
        strokeAttrs:textHintStrokeWidth="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 1970-01-01
    • 2019-01-25
    • 2011-02-19
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 1970-01-01
    相关资源
    最近更新 更多