2014-10-06 2 views

답변

2

폭을 확보하기 전에 레이아웃을 그릴 때까지 기다려야합니다. 그려지면 측정되고 너비가 저장되어 getWidth으로 전화 할 수 있습니다. 레이아웃 리스너를 추가하여 뷰가 그려 질 때까지 대기 할 수 있습니다.

final RelativeLayout content = (RelativeLayout) findViewById(R.id.content); 
content.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
    @Override 
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 
     content.removeOnLayoutChangeListener(this); 
     Log.d("sizes", String.valueOf(content.getWidth())); 
    } 
}); 
+0

이렇게하는 방법은 일반적입니까? 이것이 당신에게 고맙다는 것이 아니라면! –

+0

@ КристиянКацаров 그것이 항상 내가하는 일이고 항상 효과가 있음이 입증되었습니다. – Simas