获取TextView中文本的位置

has*_*s19 8 android textview

假设我在里面有以下文本'ADD TEST' TextView,如下所示 在此输入图像描述

正如你可以看到里面的文字TextView没有相同的宽度和高度TextView.

我想要的是获取文本中的x,y位置 TextView

Che*_*amp 6

看一下几种Paint方法:getTextBounds()measureText。我们可以使用它们来确定内文本的偏移量TextViewTextView确定内的偏移量后,如果需要,我们可以将其添加到其TextView自身的位置以确定文本的屏幕坐标。

我还发现文章“ Android 101:印刷术对于理解印刷术的某些复杂性很有用。

下面的示例在三个范围内查找文本的边界,并在文本TextViews周围绘制一个矩形。矩形包含内文本的(x,y)坐标TextView

activity_main.xml
一个简单的演示布局。

<android.support.constraint.ConstraintLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@android:color/holo_blue_light"
        android:padding="24dp"
        android:text="Hello World"
        android:textColor="@android:color/black"
        android:textSize="50sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@android:color/holo_blue_light"
        android:padding="24dp"
        android:text="Hello Worldly"
        android:textColor="@android:color/black"
        android:textSize="50sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:background="@android:color/holo_blue_light"
        android:padding="24dp"
        android:text="aaaaaaaaaa"
        android:textColor="@android:color/black"
        android:textSize="50sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView2" />

</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        drawTextBounds((TextView) findViewById(R.id.textView1));
        drawTextBounds((TextView) findViewById(R.id.textView2));
        drawTextBounds((TextView) findViewById(R.id.textView3));
    }

    private void drawTextBounds(TextView textView) {
        // Force measure of text pre-layout.
        textView.measure(0, 0);
        String s = (String) textView.getText();

        // bounds will store the rectangle that will circumscribe the text.
        Rect bounds = new Rect();
        Paint textPaint = textView.getPaint();

        // Get the bounds for the text. Top and bottom are measured from the baseline. Left
        // and right are measured from 0.
        textPaint.getTextBounds(s, 0, s.length(), bounds);
        int baseline = textView.getBaseline();
        bounds.top = baseline + bounds.top;
        bounds.bottom = baseline + bounds.bottom;
        int startPadding = textView.getPaddingStart();
        bounds.left += startPadding;

        // textPaint.getTextBounds() has already computed a value for the width of the text, 
        // however, Paint#measureText() gives a more accurate value.
        bounds.right = (int) textPaint.measureText(s, 0, s.length()) + startPadding;

        // At this point, (x, y) of the text within the TextView is (bounds.left, bounds.top)
        // Draw the bounding rectangle.
        Bitmap bitmap = Bitmap.createBitmap(textView.getMeasuredWidth(),
                                            textView.getMeasuredHeight(),
                                            Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint rectPaint = new Paint();
        rectPaint.setColor(Color.RED);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(1);
        canvas.drawRect(bounds, rectPaint);
        textView.setForeground(new BitmapDrawable(getResources(), bitmap));
    }
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 但当 TextView.gravity=center(或 end) 时它不起作用,还有其他解决方案吗? (2认同)