2011-01-17 3 views
4

내가 뷰의 높이를 얻기 위해 노력하고 있으며이 내가 시도 무엇 0안드로이드 : viewHeight를 얻을 프로그램

를 반환 유지 : 없음

View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null); 
hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight(); 

내가 또한뿐만 아니라 hourViewHeight = hourView.getHeight();을 시도했지만 즐거움.

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/mainLayout" android:background="@layout/border"> 

    <TextView android:text="12am" android:id="@+id/hourTextView" 
     android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
</RelativeLayout> 

내가 위의 XML 레이아웃의 높이를 얻으려면 basicllay :

여기
@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.calendar_day_screen); 

     Intent intent = getIntent(); 

     mDate = new DateTime(intent.getStringExtra("date")); 
     circleIcon = (ImageView) findViewById(R.id.circle); 

     mHoursLayout = (RelativeLayout) findViewById(R.id.dayLayout); 

     TopBarUtil topBar = new TopBarUtil(getApplicationContext()); 

     circleIcon.setOnClickListener(topBar.onActionClickListener()); 

     mContext = getApplicationContext(); 



     initialize(); 
    } 

내 XML 레이아웃입니다 :

내가 여기에있는 초기화() 메서드 내 두 라인 전화 하지만 항상 0을 반환합니다.

편집 : 또한 아래에서 시도 :

@Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 

     final View hourView = View.inflate(mContext, R.layout.calendar_hour_item,null); 
     ViewTreeObserver observer = hourView.getViewTreeObserver(); 

     observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

      @Override 
      public void onGlobalLayout() { 
       hourView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
       hourViewHeight = hourView.getHeight(); 
       Log.d(TAG, "hourViewHeight = " + hourViewHeight); 
      } 
     }); 

//  hourViewHeight = hourView.findViewById(R.id.mainLayout).getHeight(); 
//  Log.d(TAG, "hourViewHeight = " + hourViewHeight); 
    } 

답변

1

시도로 그 때문에이 표시됩니다, 전망은 여전히 ​​레이아웃을 수행 단계를 측정하지 않았습니다. 레이아웃에서 "동작"하는 방법에 대해서만 설명했으나 각 뷰를 배치 할 위치는 여전히 계산하지 않았습니다.

(유사 질문 : How to get the width and height of an android.widget.ImageView?) 당신이 게시 두 번째 예에서

, 나는 당신이 그렇게 활동이 따라서 그것을 그릴하지 않습니다 활동에 뷰를 추가 생각하지 않는다, 당신은 로그 메시지를 읽을하지 않습니다 . 일부 레이아웃에는 setContentView 또는 addView으로 전화하십시오.

+0

감사합니다. 너는 자리에 있었어. – jonney

-2

너는 높이를 wrap_content으로하고 그 내용을 채우지 않았습니다. 0

+0

나는 100dip과 같은 높이를 지정하려고했지만 여전히 0을 반환합니다. – jonney

+0

linearlayout의 높이를 지정하고 있습니까? –

+0

위의 XML 코드에서와 같이 relativelayout을 사용합니다. 나는 android : layout_height = "50dip"을 사용하여 높이를 지정했지만 여전히 작동하지 않습니다. 나는 또한 onStart와 심지어 diddnt 작업에 붙여진 OnGlobalLayoutListener를 사용하여 시도했다. 보기 높이를 되돌려 놓는 것이 왜 그렇게 힘든 일입니까? – jonney

6

그것이 당신이 그들을 쿼리 때문에 때 일하지 않은 첫 번째 예에서

hourView.measure(hourView.getWidth(), hourView.getHeight()); 
hourView.getMeasuredHeight() 
+0

GlobalLayoutListener에있는 경우'.measure' 메서드가 필요하다고 생각하지 않지만 표준 getHeight() 및 getWidth() 대신 getMeasuredHeight() 및 getMeasuredWidth()를 사용하는 것으로 생각됩니다. . – kcoppock

+0

나는이 코드를 시도하고 그것은 내게 감사합니다 – AndroSco

관련 문제