TextInputEditText - CompoundDrawable 未居中

Posted

技术标签:

【中文标题】TextInputEditText - CompoundDrawable 未居中【英文标题】:TextInputEditText - CompoundDrawable is not centered 【发布时间】:2019-06-19 01:57:05 【问题描述】:

我试图通过创建一个 TextDrawable 然后使用 compoundDrawable 设置它来为我的 TextInputEditText 添加一个后缀。除了可绘制对象被剪裁到组件右侧之外,一切都进行得很好。这可能是什么原因造成的?到目前为止,我尝试更改字体大小,但这并没有任何区别......drawable 太宽还是什么?

字符串是"kr/månad",你可以看到它被剪掉了..

XML

<android.support.design.widget.TextInputLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text_input_layout"
    style="@style/TextInputLayoutStyle"
    android:theme="@style/TextInputLayoutTheme">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/text_input_edit_text"
        style="@style/TextInputEditTextStyle" />

</android.support.design.widget.TextInputLayout>

组件代码

  textInputEditText.setCompoundDrawables(null, null, TextDrawable(unitText), null)

TEXTDRAWABLE

class TextDrawable(private val text: String?) : Drawable() 

    private val paint: Paint

    init 
        paint = Paint()
        paint.color = Color.BLACK
        paint.textSize = 44f
        paint.isAntiAlias = true
        paint.isFakeBoldText = true
        paint.typeface = Typeface.create("sans-serif-light", Typeface.NORMAL)
        paint.style = Paint.Style.FILL
        paint.textAlign = Paint.Align.CENTER
    

    override fun draw(canvas: Canvas) 
        text?.let  text ->
            canvas.drawText(text, 0f, 0f, paint)
        
    

    override fun setAlpha(alpha: Int) 
        paint.alpha = alpha
    

    override fun setColorFilter(cf: ColorFilter?) 
        paint.colorFilter = cf
    

    override fun getOpacity(): Int 
        return PixelFormat.TRANSLUCENT
    

【问题讨论】:

这是否适用于默认EditText @ADM 我不知道。但是我想用材质组件! 【参考方案1】:

试试这个代码:

class TextDrawable(private val text: String?) : Drawable() 

    private val paint = Paint().apply 
        color = Color.BLACK
        textSize = 44f
        isAntiAlias = true
        isFakeBoldText = true
        typeface = Typeface.create("sans-serif-light", Typeface.NORMAL)
        style = Paint.Style.FILL
        setBounds(0, 0, measureText(text).toInt(), 0)
    

    override fun draw(canvas: Canvas) 
        text?.let  text ->
            canvas.drawText(text, 0f, 0f, paint)
        
    

    override fun setAlpha(alpha: Int) 
        paint.alpha = alpha
    

    override fun setColorFilter(cf: ColorFilter?) 
        paint.colorFilter = cf
    

    override fun getOpacity(): Int 
        return PixelFormat.TRANSLUCENT
    

区别在于两行。我删除了这个

paint.textAlign = Paint.Align.CENTER  

并添加了这个:

setBounds(0, 0, measureText(text).toInt(), 0)  

【讨论】:

问题是因为我没有设置边界,这似乎是setCompoundDrawables() 计算和居中文本所必需的。

以上是关于TextInputEditText - CompoundDrawable 未居中的主要内容,如果未能解决你的问题,请参考以下文章

TextInputLayout 和 TextInputEditText 的区别

backgroundTint 属性为整个 TextInputEditText 着色

TextInputLayout 和 TextInputEditText 的简单介绍以及使用

使 TextInputEditText 只读但可点击

如何删除material.textfield.TextInputEditText(Material EditText)的边框

为啥 TextInputEditText 不能在 Android Studio 中转换为 TextInputLayout?