在android中查找显示器(屏幕)上的视图位置(位置)
我想找到视图在显示屏上的位置。
为此,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最简单的方法是使用它
View.getLocationOnScreen();
或getLocationInWindow();
如果您想要相对于根 Layou 的位置,则 查看
Easiest way is to get it using
View.getLocationOnScreen();
ORgetLocationInWindow();
And if you want position relative to root Layout then See
使用
getLeft()
、getRight()
、getTop()
和getBottom()
。这些可以在视图实例化后使用。Use the
getLeft()
,getRight()
,getTop()
, andgetBottom()
. These can be used after the view is instantiated.你写的方法足以
在屏幕上找到位置,但您所写的位置不正确。
尝试在 onWindowFocusChanged(boolean hasFocus) 方法中编写方法(view.getLeft()、view.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...
**
**
it will solve your issue.
你确实无法提前看到风景。如果可能的话,您可能希望显式使用
invalidate()
,或者您可以在onPostResume()
或onResume()
函数中检查这一点。如果您只是尝试一下并想享受线程带来的乐趣,此代码块会将您的代码放到 UI 线程上,并等待所有内容都渲染完毕,然后显示您的代码:
最后一个代码块的工作量相当大,但是它会完成工作。我通常将这样的行放在日志中(即
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 theonPostResume()
oronResume()
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:
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
)我不确定您是否可以在布局阶段完成之前获取视图的位置。布局视图后,您可以使用 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.