开发者

Finding the View location (position) on display (screen) in android

I want to find the view's position on the display screen.

To do it, I use the methods such as view.getLeft() ,view.getBottom() , view.getRight() , view.getTop(). But unfortunately, all these methods are returned 0.

Is there any alternate way to find the view's positions (i.e coordinates on screen)?

My layout main.xml:

<TextView android:id="@+id/tv1"
 开发者_Go百科   android:layout_width="200px"
    android:layout_height="30px"
    android:text="welcome to" />

<TextView android:id="@+id/tv2"
    android:layout_width="200px"
    android:layout_height="30px"
    android:text=" Make Eit solution " />

And my class has this in onCreate():

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);

System.out.println("tv4 width:"+tv2.getWidth());
System.out.println("tv4 height:"+tv2.getHeight());
System.out.println("Right:"+tv2.getRight());
System.out.println("Left:"+tv2.getLeft());
System.out.println("Top:"+tv2.getTop());
System.out.println("Bottom:"+tv2.getBottom());


Easiest way is to get it using View.getLocationOnScreen(); OR getLocationInWindow();

And if you want position relative to root Layout then See


Use the getLeft(), getRight(), getTop(), and getBottom(). These can be used after the view is instantiated.


the methods u have written is enough to find the location on screen, but the place where you have written is not correct.

try to write the methods (view.getLeft(), view.getTop()... etc) in onWindowFocusChanged(boolean hasFocus) method.

for example...

**

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
                System.out.println("Right:"+tv2.getRight());
                System.out.println("Left:"+tv2.getLeft());
                System.out.println("Top:"+tv2.getTop());
        }
    }

**

it will solve your issue.


You really can't get the view before hand. You may want to do an explicit using invalidate() if possible or you can check this in the onPostResume() or onResume() function.

If you're just trying things out and want to have fun with threads, this code block will put your code onto the UI thread and will wait for everything to be rendered and then display your code:

final View gv = this;
ViewTreeObserver vto = gv.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @SuppressLint("NewApi")
    public void onGlobalLayout() {
        gv.getViewTreeObserver().removeGlobalOnLayoutListener(this);

        tv1=(TextView)findViewById(R.id.tv1);
        tv2=(TextView)findViewById(R.id.tv2);
        System.out.println("tv4 width:"+tv2.getWidth());
        System.out.println("tv4 height:"+tv2.getHeight());
        System.out.println("Right:"+tv2.getRight());
        System.out.println("Left:"+tv2.getLeft());
        System.out.println("Top:"+tv2.getTop());
        System.out.println("Bottom:"+tv2.getBottom());
}

This last one is pretty heavy duty lifting but it will get the job done. I usually put any lines like this in my logs(i.e. android.util.Log)


I'm not sure that you can get the position of the view before the layout phase have been done. Once the view is layed out you can use getLocationOnScreen to get the position of the view. getTop and similar only returns the position of the view in its parent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜