看看几个Paint方法:

getTextBounds()和

measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextView本身的位置,以确定文本的屏幕坐标(如果需要).

以下示例在三个TextView中查找文本的边界,并在文本周围绘制一个矩形.矩形包含TextView中文本的(x,y)坐标.

activity_main.xml中

一个简单的演示布局.

android:id="@+id/layout"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

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" />

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" />

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" />

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));

}

}

android 获取布局textview,android – 获取TextView中文本的位置相关推荐

  1. android 设置布局宽度,Android布局宽度为50%

    我使用相对布局来创建XML的Android UI.布局以ImageView和TextView为中心.我需要将这两个元素放在彼此的上方和下方.我想这样做,这两个元素占整个宽度的50%.我尝试了一个带有a ...

  2. HTML自动获取地址,网页中自动获取经纬度值并在地图中显示当前位置实例代码...

    下面的HTML代码就是实例完整代码,如果你需要本项目完整实例代码,下载链接在文章最底部 网页中自动获取经纬度值并在地图中显示当前位置,可在地图中进行拖动选择当前位置并输出经纬度,这是项目实例代码,可参 ...

  3. VB 获取光标在TextBox、RichTextBox中所在的位置

    '支持中文,一个汉字算一列 Option Explicit Public Const WM_USER = &H400 Public Const EM_EXGETSEL = WM_USER + ...

  4. android textview坐标,android – 获取TextView中文本的位置

    看看几个Paint方法: getTextBounds()和 measureText.我们可以使用它们来确定TextView中文本的偏移量.确定TextView中的偏移后,我们可以将其添加到TextVi ...

  5. android横向多布局约束,Android约束布局中心水平对齐Textview和Imageview不工作

    参见英文答案 > Constraint Layout Vertical Align Center                                    6个 我希望我的textv ...

  6. Android百分比布局支持和垂直TextView

    In this tutorial, we'll discuss and implement Android Percent Layout Support Library. Furthermore, w ...

  7. android如何引用布局,android 动态布局与引用第三方layout中的布局

    引用第三方layout中的布局LinearLayout rightContentView = (LinearLayout) findViewById(R.id.lyt_chat_content); V ...

  8. android 单元布局,在Android布局中挖洞

    试试这个:(中的onCreate) class FL extends FrameLayout { private List mViews = new ArrayList(); private Bitm ...

  9. android局部布局替换,Android 局部布局替换的实现方式

    最近再搞远程视频的功能,其中要实现加载视频.加载视频失败.加载成功的局部布局替换,查阅相关资料,找到一种投机取巧的方式. 首先分别写这三种效果的子布局,分别为 top_remotetreate.xml ...

最新文章

  1. developer console 学习
  2. 开发一个出生年份的下拉选择框供用户选择_你的下拉式菜单设计对了吗?
  3. 【bzoj3631】[JLOI2014]松鼠的新家
  4. mysql与php6_PHP与MySQL的连接
  5. 使用AIDL实现进程间的通信
  6. DataTable类(MSDN)
  7. Unity之如何使用夜神模拟器logcat
  8. 环回测试能够提供什么信息_X射线无损检测能够提供BGA焊点的重要信息
  9. python模块:网络协议和支持
  10. 友华pt926g超级密码_获取电信PT926G光猫超级管理员及账号密码
  11. 论文里引用专利参考文献怎么写?
  12. python第六周项目华容道_华容道游戏(中)
  13. Excel 2010 VBA 入门 135 利用窗体制作登录界面
  14. 7-1 sdust-Java-字符串集合求并集 (20 分)
  15. OSS服务和自建服务器存储对比
  16. 人工神经网络的结构基本上分为两类
  17. CorelDRAW教程分享:绘制流程图简单方法分享
  18. Conflux 请你“出名”啦!
  19. Editplus各个版本(最新版本是5.3)注册码下载
  20. 校园二手物品交易平台

热门文章

  1. html服务器显示,html显示服务器盘符
  2. c语言的文案,点心回顾 | 这是一个充满有趣灵魂的C语言乐园!
  3. python随机生成一个地区地址_为特定地区/国家生成随机坐标的轻量级工具?
  4. berkeley db mysql_BDB:源自 Berkeley DB,事务型数据库
  5. 质数c语言欧拉筛选,Python|欧拉筛法求质数
  6. 多平台、高颜值的网易云第三方播放器
  7. 据说这是中途接手别人项目时的场景
  8. 皮一皮:被看穿的既视感...
  9. 如何将 Spring Boot Actuator 的指标信息输出到 InfluxDB 和 Prometheus
  10. 10a 16a 插座区别_10A插座和16A插座有什么区别?