方法得到的视图返回位置0 [英] Methods to get position of view returns 0
问题描述
这个问题已经被问了好几次,但每个人都给出了,为什么发生这种情况的原因(即布局奠定了之前发生计算)。但我需要这个解决方案。我试过 getBottom();
,共达();
, getLocationOnScreen将(INT []的位置);
。但是,所有返回值零(0)。我甚至尝试在 ONSTART()给这些;
,以便有时间布局铺设,但没有运气。
这是我的code:
This question has been asked several times, but everyone gives the reason for why this occurs (i.e. calculation occurs before the layout is laid). But I need the solution for this. I tried getBottom();
, getTop();
, getLocationOnScreen(int[] location);
. But all returns the value Zero (0). I even tried giving these in onStart();
, to give time for layout to be laid, but no luck.
Here is my code:
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
Log.d("getBottom :", Integer.toString(tv.getBottom()));
Log.d("gettop :", Integer.toString(tv.getTop()));
int[] location = new int[2];
tv.getLocationOnScreen(location);
Log.d("getlocationonscreen values :", Integer.toString(location[0])+" "+Integer.toString(location[1]));
}
@Override
protected void onStart() {
Log.d("getBottom in onStart :", Integer.toString(tv.getBottom()));
Log.d("gettop in onStart :", Integer.toString(tv.getTop()));
int[] location = new int[2];
tv.getLocationOnScreen(location);
Log.d("getlocationonscreen in onStart :", Integer.toString(location[0])+" "+Integer.toString(location[1]));
super.onStart();
}
布局XML:
Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="156dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
此外,我对重复的问题深表歉意。先谢谢了。
Again, I apologize for repeating the question. Thanks in advance.
推荐答案
您把测量中的<一个href="http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged%28boolean%29"><$c$c>onWindowFocusChanged()$c$c>-method.
正如文件指出:
You put the 'measuring' in the onWindowFocusChanged()
-method.
As the documentation states:
这是这种活性是否是对用户可见的最佳指标。
This is the best indicator of whether this activity is visible to the user.
您也可以把它放在<一href="http://developer.android.com/reference/android/app/Activity.html#onResume%28%29"><$c$c>onResume()$c$c>这是在申请前的最后一个步骤是完全在屏幕上和主动,但是:
You could also put it in the onResume()
which is the last step before the application is completely on screen and active, however:
请记住,onResume不是你的活动是对用户可见的最佳指标;一个系统窗口,如键盘锁可能是在前面。使用onWindowFocusChanged(布尔值)要肯定知道您的活动是用户可见的(例如,恢复一个游戏)。
Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged(boolean) to know for certain that your activity is visible to the user (for example, to resume a game).
如果尚未显示的窗口/视图但不保证,它有其测量结果,因此,previous方法会更好。
If the window/view has not yet been displayed there is no guarantee that it has its measurements, thus the previous method would be better.
这篇关于方法得到的视图返回位置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!