如何在Android中找到Textview的开始和结束标记?如下图所示?
我正在尝试这个this
使用方法
getLocationInWindow (int[] location)
getLocationOnScreen (int[] location)
但没有得到正确的结果..
任何帮助将不胜感激..谢谢:)
答案 0 :(得分:3)
设x1和y1是文本视图的起始坐标,然后它的值将是:
TextView textview = (TextView)findViewbyId(R.id.textview1);
int x1=textview.getLeft();
int y1=textview.getTop();
让x2和y2是文本视图的结束坐标,然后它的值将是:
int x2=textview.getRight();
int y2=textview.getBottom();
答案 1 :(得分:1)
如果您需要坐标为根相对(即。(0,0)是应用程序窗口的左上角像素),则需要执行以下操作:
private Rect getScreenXY(Context context, View view, View rootView) {
int[] coords = new int[2];
// This returns the screen-space location of the upper left corner of the view
view.getLocationOnScreen(coords);
// This rect is relative to the screen
Rect rootRelativeRect = new Rect(coords[0], coords[1], view.getWidth() + coords[0], view.getHeight() + coords[1]);
DisplayMetrics displayMetrics = new DisplayMetrics();
((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(displayMetrics);
// To get root-relative coordinates we need to add the status bar's height
rootRelativeRect.offset(0, rootView.getMeasuredHeight() - displayMetrics.heightPixels);
return rootRelativeRect;
}
答案 2 :(得分:0)
getX()+ getWidth()是你的结束X坐标,getY()+ getHeight()是你的结束Y坐标