2012-01-16 4 views
8

이 질문은 여러 번 요청되었지만 모든 사람이 왜 이런 일이 발생했는지 (레이아웃을 적용하기 전에 계산이 이루어짐) 이유를 설명합니다. 그러나 나는 이것을위한 해결책이 필요하다. 나는 getBottom();, getTop();, getLocationOnScreen(int[] location);을 시도했다. 그러나 모두는 값 Zero (0)을 반환합니다. 나는 심지어 레이아웃을 놓을 시간을주기 위해 onStart();에 이것을 주려고했지만 행운이 없었다.보기의 위치를 ​​가져 오는 방법은 0을 반환합니다.

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 :

<?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> 

가 다시, 나는이 질문을 반복 사과 다음은 내 코드입니다. 미리 감사드립니다.

+0

정확한 문제를 조금 더 설명해 주실 수 있습니까? 위의 코드에서 잘못된 것. –

답변

13

onWindowFocusChanged() -method에 '측정'을 넣습니다. 문서 상태로
는 :

이이 활동이 사용자에게 표시되는지 여부의 가장 좋은 지표입니다.

또한 응용 프로그램 전에 마지막 단계 인 onResume()에 넣을 수는 있지만, 화면 및 활성에 완전히 :

onResume 그 활동 최선 표시하지 있음을 유의하십시오 사용자가 볼 수 있습니다. 키 가드와 같은 시스템 윈도우가 앞에있을 수 있습니다. onWindowFocusChanged (boolean)를 사용하여 사용자가 볼 수있는 작업 (예 : 게임 재개)을 알 수 있습니다.

창 /보기가 아직 표시되지 않은 경우 측정 값이 있다는 보장이 없으므로 이전 방법이 더 좋을 것입니다.

관련 문제