Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中查找显示器(屏幕)上的视图位置(位置)_Android_Android Layout_Android View - Fatal编程技术网

在android中查找显示器(屏幕)上的视图位置(位置)

在android中查找显示器(屏幕)上的视图位置(位置),android,android-layout,android-view,Android,Android Layout,Android View,我想在显示屏上找到视图的位置 为此,我使用了一些方法,如view.getLeft(),view.getBottom(),view.getRight(),view.getTop()。但不幸的是,所有这些方法都返回0 是否有其他方法可以找到视图的位置(即屏幕上的坐标) 我的布局main.xml: <TextView android:id="@+id/tv1" android:layout_width="200px" android:layout_height="30px"

我想在显示屏上找到视图的位置

为此,我使用了一些方法,如
view.getLeft()
view.getBottom()
view.getRight()
view.getTop()
。但不幸的是,所有这些方法都返回
0

是否有其他方法可以找到视图的位置(即屏幕上的坐标)

我的布局
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 " />

在布局阶段完成之前,我不确定您是否可以获得视图的位置。布局视图后,您可以使用
getLocationOnScreen
获取视图的位置
getTop
和类似命令仅返回视图在其父视图中的位置。

使用
getLeft()
getRight()
getTop()
getBottom()
。这些可以在视图实例化后使用。

您真的无法提前获得视图。如果可能,您可能希望使用
invalidate()
执行显式操作,或者您可以在
onpostsresume()
onResume()
函数中进行检查

如果您只是在尝试一些东西,并且希望从线程中获得乐趣,则此代码块将把您的代码放到UI线程中,并等待所有内容呈现,然后显示您的代码:

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

最后一个是相当重的负载,但它会完成工作。我通常会在我的日志中放入任何类似的行(即,
android.util.Log

您编写的方法足以 找到屏幕上的位置,但您书写的位置不正确

尝试在onWindowFocusChanged(boolean hasFocus)方法中编写方法(view.getLeft()、view.getTop()…等)

例如

**

**


它将解决您的问题。

最简单的方法是使用
View.getLocationOnScreen()
getLocationInWindow()


如果你想知道相对于根布局的位置,那么我在这里找到了答案。[Get coordinates of View][1][1]:这些方法给出了相对于其父对象的视图位置。@ilkinulas那么相对于设备的解决方案是什么呢?这是这个问题的完美答案。+1
getLeft()
getRight()
不会每次都起作用,因为它们返回相对于父对象的位置,不是屏幕。
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());
}
@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());
        }
    }