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

发布于 2024-10-14 16:40:20 字数 1058 浏览 4 评论 0原文

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

为此,我使用 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 " />

我的类在 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());

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

旧时光的容颜 2024-10-21 16:40:20

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

如果您想要相对于根 Layou 的位置,则 查看

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

And if you want position relative to root Layout then See

南风几经秋 2024-10-21 16:40:20

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

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

绾颜 2024-10-21 16:40:20

你写的方法足以
在屏幕上找到位置,但您所写的位置不正确。

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

例如...

**

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

**

它将解决您的问题。

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.

雨巷深深 2024-10-21 16:40:20

你确实无法提前看到风景。如果可能的话,您可能希望显式使用 invalidate(),或者您可以在 onPostResume()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

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)

下壹個目標 2024-10-21 16:40:20

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文