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

♀尐吖头ヾ 提交于 2020-01-10 03:50:12

问题


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

回答1:


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

And if you want position relative to root Layout then See




回答2:


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




回答3:


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.




回答4:


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)




回答5:


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.



来源:https://stackoverflow.com/questions/4841391/finding-the-view-location-position-on-display-screen-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!