2013-04-26 4 views
0

아래 레이아웃에서 EditText (파란색 배경색)이 보이지 않으면 너비가 0이고 회 전자가 모든 공간을 차지합니다. 왜?레이아웃의 요소가 가중치에 따라 크기가 조정되지 않음

<TableRow 
    android:layout_width="match_parent" 
    android:gravity="center_vertical" > 

    <TextView 
     style="@style/Label.Plain" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="3dp" 
     android:layout_weight="0" 
     android:text="@string/Distance" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:orientation="horizontal" > 

     <EditText 
      android:id="@+id/et_hfDistance" 
      android:layout_width="fill_parent" 
      android:layout_height="@dimen/button_height" 
      android:layout_weight="1" 
      android:background="#ff0000ff" 
      android:inputType="numberDecimal" 
      android:selectAllOnFocus="true" 
      android:textSize="@dimen/spinner_text" /> 

     <Spinner 
      android:id="@+id/sp_hfDistanceUnits" 
      style="@style/Spinner" 
      android:layout_width="fill_parent" 
      android:layout_weight="0" 
      android:background="#ff00ff00" 
      android:padding="0dp" /> 
    </LinearLayout> 
</TableRow> 
당신은 두 아이의 부모 android:layout_width="fill_parent" 및 이와 유사한 폭을 설정하기 때문에

답변

2

은, 무게는 역으로 고려됩니다. 회 전자의 실제 중량 = 무한 1/0 반면

real weight = (1/weight)/sum((1/weight) for weight in the layout) 

따라서 글고의 실제 중량은 1이다. 이에 비해 EditText는 더 이상 보이지 않으며 스피너는 모든 공간을 차지합니다.

는이 동작을 변경 스피너와 글고 android:layout_width="0dp"

+0

0dp'와'fill_parent' '의 차이는 무엇 모두를 변경하려면? –

+0

너비가'fill_parent' 일 때 가중치는 그다지 관련이 없기 때문에 이것은별로 좋은 설명이 아닙니다. 메소드 ['getChildMeasureSpec'] (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/view/ViewGroup.java#ViewGroup .getChildMeasureSpec % 28int % 2Cint % 2Cint % 29)는 비난입니다. 본질적으로 두 어린이는 전체 너비로 측정 된 다음 레이아웃에서 나타나는 순서대로 다른 하나의 위에 그립니다. 'layout_width'를'wrap_content'로 변경하면 문제가 해결되지만 불필요한 측량 패스가 발생합니다. '0dp'는 그것을 피합니다. – Delyan

+0

'fill_parent' 또는 비슷하게'match_parent' (둘 다 동일 함)는 레이아웃을 부모의 너비와 정확히 일치하도록 요청하고 있습니다. 이 경우 가중치는 이러한 너비의 약수와 같습니다. 나는 이유를 모른다. '0dp'는 기본적으로 너비가 0임을 의미하며, 가중치는 모든 레이아웃을 채우기 위해이 크기를 증가시키는 역할을합니다. –

관련 문제