简体   繁体   English

setCompoundDrawables EditText Text Overlapping

[英]setCompoundDrawables EditText Text Overlapping

I'm Implementing a Drawable as text at the start of my EditText using setCompoundDrawables but I'm getting incorrect results. 我正在使用setCompoundDrawablesEditText的开头实现Drawable作为文本,但是我得到的结果不正确。 What I need is for my text to start after the Drawable when typing but in stead I'm getting this 我需要的是我的文字在Drawable在打字时开始,而不是我得到这个

在此输入图像描述

I have a EditText In a layout defined like this 我有一个EditText在这样定义的布局中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="4dp" >

    <EditText
        android:id="@+id/field"
        style="@style/TextField"
        android:inputType="number" />

</LinearLayout>

I have to add a piece of text as a Drawable in the beginning of the EditText . 我必须在EditText的开头添加一段文本作为Drawable So I convert Text to a Drawable using this class 所以我使用这个class将Text转换为Drawable

public class TextDrawable extends Drawable {

    private final String text;
    private final Paint paint;

    public TextDrawable(String text) {
        this.text = text;
        this.paint = new Paint();
        paint.setColor(Color.parseColor("#009999"));
        paint.setTextSize(30);
        paint.setAntiAlias(true);
        paint.setTextAlign(Paint.Align.LEFT);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.drawText(text, 0, 6, paint);
    }

    @Override
    public void setAlpha(int alpha) {
        paint.setAlpha(alpha);
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        paint.setColorFilter(cf);
    }

    @Override
    public int getOpacity() {
        return PixelFormat.TRANSLUCENT;
    }
}

And I implement it here 我在这里实现它

final   EditText field   = (EditText) widget.findViewById(R.id.field);

field.setCompoundDrawables(new TextDrawable(this.label), null, null, null);
field.setText(this.value);

Am I doing something wrong? 难道我做错了什么?

EDIT 编辑

I tried using this method field.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_drawable, 0, 0, 0); 我尝试使用此方法field.setCompoundDrawablesWithIntrinsicBounds(R.drawable.my_drawable, 0, 0, 0); and it works but obviously I can't use it because the drawable isn't defined as a resource. 它工作但很明显我不能使用它因为drawable没有被定义为资源。

所以你的Drawable的ctor中的setBounds,或覆盖getIntrinsicWidth(),你需要检查哪一个工作

You can do like that 你可以这样做

<EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@drawable/ic_launcher" >

Try do like this- 尝试这样做 -

field.setCompoundDrawablesWithIntrinsicBounds(new TextDrawable(this.label), null, null, null);

You have to set the bounds. 你必须设置界限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM