2011-11-17 16 views
46

Sto costruendo un'interfaccia utente, ed è tutto statico definito nell'XML. Tutto ha pesi dappertutto, e mentre sembra giusto, volevo vedere che le cose in realtà hanno la giusta altezza e tutto il resto. Il problema è che non importa dove chiamo .getHeight() per il mio layout di formato ho ottenuto 0. Ho provato sia su onCreate() che onStart(). Stessa cosa. Succede anche per tutti gli oggetti dell'interfaccia utente. Qualche idea?getHeight restituisce 0 per tutti gli oggetti dell'interfaccia utente Android

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

Possibile duplicato del [getWidth() e getHeight() di vista restituisce 0] (https://stackoverflow.com/questions/3591784/getwidth -e-getheight-of-view-returns-0) –

risposta

36

In breve, i punti di vista non sono ancora costruito nel onCreate(), onStart(), o onResume(). Dal momento che tecnicamente non esistono (per quanto riguarda il ViewGroup), le loro dimensioni sono 0.

A lungo, puoi andare qui per una spiegazione migliore su come gestirlo.

How to retrieve the dimensions of a view?

63

E '0 perché in entrambi i onCreate e onStart, la vista non è in realtà stato ancora redatto. È possibile aggirare questo ascoltando per quando la vista è in realtà redatto:

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); 
      } 
     }); 

La chiamata per rimuovere l'ascoltatore è quello di evitare ripetute invocazioni del vostro gestore personalizzato sulle modifiche del layout ... se si vuole ottenere quelle , puoi ometterlo.

+7

In quale metodo Activity lifecyle dovrebbe essere implementato? Sto ancora ricevendo 0 per tv.getHeight() –

+0

@IgorGanapolsky, questo metodo ha funzionato per il mio frammento in onCreateView. Per un'attività normale, prevedo che onCreate sarebbe appropriato. – VinceFior

+5

'removeGlobalOnLayoutListener' è deprecato oggigiorno, dovresti usare' removeOnGlobalLayoutListener'. – vedi

1

Ottenendo il pulsante in una vista si può facilmente recuperare la sua altezza ecc È possibile definire un onClickListener in questo modo:

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(); 
}}; 

Spero che questo aiuti.

0

Usare questa funzione per ottenere altezza o larghezza di vista

private int getHeightOfView(View contentview) { 
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
    //contentview.getMeasuredWidth(); 
    return contentview.getMeasuredHeight(); 
} 
Problemi correlati