2010-10-15 30 views
5

Aileyi doldurmak için kurulumun genişliğini eşleştirmek için bir düğmenin yüksekliğini ayarlamak istediğim android XML düzeninde çalışıyorum. Açıkçası bu sayı ekran boyutuna göre değişecektir, bu yüzden ayarlanmış bir piksel boyutunu kullanamıyorum. Birisi ekran genişliğine göre düğme genişliğini elde etmeme yardımcı oluyor ve bunu yükseklik ayarına geçiriyor mu? Android: Genişliğini temel alarak XML'de düğme yüksekliğini ayarlama

yerine PX tarafından Piksel boyutunu kullanmak yerine, daldırma (yani aygıt bağımsız piksel), daldırma bağımsız cihazın ekran boyutuna göre piksel olarak boyutunu alacak söz,

cevap

-1

Josh teşekkür ederim.

örneğin: android: TEXTSIZE = "12dip"

Sen daldırma veya dp kullanabilir.

Tadını çıkarın!

3

Bir zamanlar benzer bir sorun yaşadım ve sadece XML'den çalışan bir çözüm buldum. Kendi Button-Class'ınızı yazmalı ve [onMeassure] [1] metodunun üzerine yazmalısınız.

Örnek:

/** 
* @see android.view.View#measure(int, int) 
*/ 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); 
} 

private int width; // saves the meassured width 


/** 
* Determines the width of this view 
* 
* @param measureSpec 
*   A measureSpec packed into an int 
* @return The width of the view, honoring constraints from measureSpec 
*/ 
private int measureWidth(int measureSpec) { 
    int result = 30; 
    int specMode = MeasureSpec.getMode(measureSpec); 
    int specSize = MeasureSpec.getSize(measureSpec); 

    if (specMode == MeasureSpec.EXACTLY) { 
     // We were told how big to be 
     result = specSize; 
    } else { 
     result =1123; // meassure your with here somehow 
     if (specMode == MeasureSpec.AT_MOST) { 
      // Respect AT_MOST value if that was what is called for by measureSpec 
      result = Math.min(result, specSize); 
     } 
    } 
      width = result; 
    return result; 
} 

/** 
* Determines the height of this view 
* 
* @param measureSpec 
*   A measureSpec packed into an int 
* @return The height of the view, honoring constraints from measureSpec 
*/ 
private int measureHeight(int measureSpec) { 
    int result = 0; 
    int specMode = MeasureSpec.getMode(measureSpec); 
    int specSize = MeasureSpec.getSize(measureSpec); 

    if (specMode == MeasureSpec.EXACTLY) { 
     // We were told how big to be 
     result = specSize; 
    } else { 
     result = width; 
     if (specMode == MeasureSpec.AT_MOST) { 
      // Respect AT_MOST value if that was what is called for by measureSpec 
      result = Math.min(result, specSize); 
     } 
    } 

    return result; 
} 

[1]: http://developer.android.com/reference/android/view/View.html#onMeasure(int, int)

İlgili konular