2011-11-17 19 views
46

Bir kullanıcı arayüzü yapıyorum ve XML'de statik olarak tanımlanmış. Tüm bunların her yerinde ağırlıkları var, ve doğru görünse de, gerçekte her şeyin doğru yüksekliğe sahip olduğunu görmek istedim. Sorun şu ki, format düzenimi için .getHeight() 'ı çağırdığım yerde 0 aldım. Hem onCreate() hem de onStart()' da denedim. Aynı şey. Tüm UI nesneleri için de geçerlidir. Herhangi bir fikir?getHeight, tüm Android UI nesneleri için 0 değerini döndürür

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: Kısacası

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

Olası kopyalar [Görünüm döndürür ait GetWidth() ve GetHeight() 0] (https://stackoverflow.com/questions/3591784/getwidth -ve-get-get-of-görünüm-döndürür-0) –

cevap

36

, görünümler onCreate henüz inşa edilmemiş(), onStart() veya onResume(). Teknik olarak mevcut olmadıklarından (ViewGroup ilgili olduğu sürece), boyutları

Uzun süre burada nasıl ele alınacağına dair daha iyi bir açıklama için buraya gidebilirsiniz.

How to retrieve the dimensions of a view?

63

onCreate ve onStart hem görünüm aslında henüz çizilmiş değil çünkü 0 var. Sen görünümü aslında çizildiğinde dinleyerek bu sorunun üstesinden gelebilirsiniz:

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

çağrı dinleyici kaldırmak için düzen değişiklikleri özel işleyici tekrarlanan çağırmaları önlemek için ... o almak istiyorsanız orada Onu ihmal edebilirsin. Bu yardımcı olur

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

Hope: kolayca Çok gibi bir onClickListener tanımlayabilirsiniz vb onun yüksekliğini getirebilir bir View daki düğmeye elde ederek

+7

Bu hangi Activity lifeecyle yöntemi uygulanmalıdır? Ben hala tv.getHeight() –

+0

@ IgorGanapolsky için 0 alıyorum, bu yöntem onCreateView içinde benim parçası için çalıştı. Normal bir etkinlik için onCreate'ın uygun olacağını umuyorum. – VinceFior

+5

'removeGlobalOnLayoutListener' şu anda kullanımdan kaldırılmıştır, bunun yerine' removeOnGlobalLayoutListener' kullanmalısınız. – vedi

1

.

0

Bu fonksiyonu View Yükseklik veya genişliği almak için

private int getHeightOfView(View contentview) { 
    contentview.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); 
    //contentview.getMeasuredWidth(); 
    return contentview.getMeasuredHeight(); 
} 
ait
İlgili konular