2011-11-17 3 views
46

UI를 작성 중이며 XML에 정적으로 정의되어 있습니다. 그것의 모두는 장소 전체에 무게를 가지고 있고, 그것이 옳았던 동안, 나는 물건이 실제로 적당한 높이와 누구나를 가지고 있는지에 관해 알고 싶었다. 문제는 내가 어디서 .getHeight() 내 서식 레이아웃에 대한 호출이 0 있어요. 나는 onCreate() 및 onStart() 시도했다. 같은 것. 모든 UI 개체에도 적용됩니다. 어떤 생각?getHeight는 모든 Android UI 객체에 대해 0을 반환합니다.

package com.app.conekta; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.Toast; 

public class Conekta extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

    } 

    @Override 
    public void onStart() { 

     super.onStart(); 


    } 

    @Override 
    public void onResume() { 
     super.onResume(); 

     FrameLayout fl1 = (FrameLayout) findViewById(R.id.headerFrameLayout); 
     FrameLayout fl2 = (FrameLayout) findViewById(R.id.footerFrameLayout); 
     Button b=(Button) findViewById(R.id.searchButton); 

     Log.d("CONEKTA", String.valueOf(b.getHeight())); 

    } 
} 

XML : 짧은에서

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 



    <FrameLayout 
     android:id="@+id/headerFrameLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" 
     android:background="#597eAA" > 

     <ImageView 
      android:id="@+id/logoImage" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#7ba1d1" 
      android:src="@drawable/logo_conekta" /> 
    </FrameLayout> 

    <LinearLayout 
     android:id="@+id/bodyLinearLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.7" 
     android:background="#f3f3f3" 
     android:orientation="horizontal" > 

     <FrameLayout 
      android:id="@+id/leftBlankFrameLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.15" 
      android:background="#f3f3f3" > 
     </FrameLayout> 

     <LinearLayout 
      android:id="@+id/centerVerticalLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.7" 
      android:orientation="vertical" > 

      <FrameLayout 
       android:id="@+id/topCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.35" > 
      </FrameLayout> 

      <TextView 
       android:id="@+id/venueLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="What are you looking for?" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="#000000" /> 

      <EditText 
       android:id="@+id/venueTextField" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" > 

       <requestFocus /> 
      </EditText> 

      <FrameLayout 
       android:id="@+id/middleCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.05" > 
      </FrameLayout> 

      <TextView 
       android:id="@+id/locationLabel" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="Where?" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:textColor="#000000" /> 

      <AutoCompleteTextView 
       android:id="@+id/locationTextField" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.025" 
       android:text="" /> 

      <LinearLayout 
       android:id="@+id/buttonLinearLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.05" 
       android:background="#f3f3f3" 
       android:orientation="horizontal" > 

       <FrameLayout 
        android:id="@+id/leftButtonLinearLayout" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.1" > 
       </FrameLayout> 

       <Button 
        android:id="@+id/searchButton" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.8" 
        android:background="#6fa8dc" 
        android:text="Search" /> 

       <FrameLayout 
        android:id="@+id/rightButtonLinearLayout" 
        android:layout_width="wrap_content" 
        android:layout_height="fill_parent" 
        android:layout_weight="0.1" > 
       </FrameLayout> 
      </LinearLayout> 

      <FrameLayout 
       android:id="@+id/bottomCenterFrameLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_weight="0.35" > 
      </FrameLayout> 
     </LinearLayout> 

     <FrameLayout 
      android:id="@+id/rightBlankFrameLayout" 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:layout_weight="0.15" 
      android:background="#f3f3f3" > 
     </FrameLayout> 
    </LinearLayout> 

    <FrameLayout 
     android:id="@+id/footerFrameLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.15" 
     android:background="#7ba1d1" > 
    </FrameLayout> 

</LinearLayout> 
+1

가능한 복제 [보기 반환의 getWidth()와 getHeight() 0] (https://stackoverflow.com/questions/3591784/getwidth - and-getheight-of-view-returns-0) –

답변

36

은,보기가에서 onCreate 아직 건설되지 않은(), ONSTART(), 또는 onResume(). ViewGroup에 관한 한 기술적으로 존재하지 않으므로 크기는 0입니다.

길게는 처리 방법에 대한 자세한 정보를 보려면 여기를 클릭하십시오.

How to retrieve the dimensions of a view?

63

에서 onCreate와 ONSTART 모두에서, 뷰가 실제로 아직 인출되지 않았기 때문에은 0입니다. 당신은보기가 실제로 그려 때를 수신하여이 문제를 가져올 수 있습니다

final TextView tv = (TextView)findViewById(R.id.venueLabel); 
final ViewTreeObserver observer= tv.getViewTreeObserver(); 
     observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
       tv.getHeight() 
       observer.removeGlobalOnLayoutListener(this); 
      } 
     }); 

호출을 리스너를 제거하기 위해 레이아웃 변경에 사용자 정의 핸들러의 반복 호출을 방지하기 위해 ... 당신이 그를 얻으려면이 , 당신은 그것을 생략 할 수 있습니다. 이 도움이

Button b=(Button) findViewById(R.id.searchButton); 
b.setOnClickListener(ButtonClick); 
android.view.View.OnClickListener ButtonClick= new android.view.View.OnClickListener() { 
    public void onClick(View v) { 
v.getHeight(); 
}}; 

희망 : 당신은 쉽게 당신은 너무 같은 onClickListener을 정의 할 수 있습니다 등 높이를 가져올 수있는보기에서 버튼을 얻어서

+7

어떤 활동 라이프 사이클 방법을 구현해야합니까? tV.getHeight()에 대해 여전히 0을 얻고 있습니다. –

+0

@IgorGanapolsky,이 메서드는 onCreateView에서 내 조각을 처리했습니다. 정상적인 활동을 위해서는 onCreate가 적절할 것으로 기대합니다. – VinceFior

+5

'removeGlobalOnLayoutListener'는 요즘 사용되지 않으므로'removeOnGlobalLayoutListener'를 대신 사용해야합니다. – vedi

1

.

0

이 기능을 사용하여보기의 높이 또는 폭을 얻을 수

private int getHeightOfView(View contentview) { 
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
    //contentview.getMeasuredWidth(); 
    return contentview.getMeasuredHeight(); 
} 
관련 문제