2013-11-26 132 views
0

如何查找Android中Textview的開始和結束座標?如下圖所示?開始和結束Android中的Textview的X-Y座標

enter image description here

我這個this

使用方法

getLocationInWindow (int[] location) 
getLocationOnScreen (int[] location) 

,但沒有得到正確的結果..

任何幫助將不勝感激..謝謝:)

嘗試
+0

你要什麼座標爲相?父視圖或應用程序的屏幕? – Rosomack

+0

頂部,左側,底部,右側這將基於視圖所在的父視圖給出界限。 – Triode

回答

0

getX()+ g etWidth()是您的末端X座標,getY()+ getHeight()是您的末端Y座標。

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; 
    } 
+0

這適用於我,其他solucions工作時,他們是在根視圖和否嵌套視圖 – Javier

2

令x1和y1開始文本視圖的cordinates那麼它的價值將是:

TextView textview = (TextView)findViewbyId(R.id.textview1); 

    int x1=textview.getLeft(); 
    int y1=textview.getTop(); 

讓X 2和Y文本視圖的結束cordinates那麼它的價值將是:

int x2=textview.getRight(); 
    int y2=textview.getBottom();