2011-02-27 2 views
2

나는 내 인생을 감당할 수 없다.Android 레이아웃 문제

이미지 뷰 (충전 가능한 모든 공간) 텍스트 뷰 글고 치기가

(등 간격) 버튼 버튼 버튼 (수직 필요에 따라 공간을 복용) (필요에 따라 수직 공간을 복용) : 나는 (수직) 다음과 같은 레이아웃을 시도하고있다

다음 레이아웃을 설정했습니다. 이미지보기는 현재 200dip으로 설정되어 있습니다. 사용 가능한 모든 공간을 채우기 위해이 값은 무엇이되어야합니까? 다른 구성 요소가 먼저 와야하고 이미지보기는 남아있는 것을 가져와야합니다. 나는 이것에 대한 예를 찾고 아직 아무것도 찾지 못했다. 당신이 필요로하는 모든이 남은 공간을 취할 수있는 ImageView에 적절한 무게와 LinearLayout처럼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" > 

    <ImageView android:id="@+id/imgView" 
       android:layout_width="fill_parent" 
       android:layout_height="200dip"    
       android:layout_alignParentTop="true" /> 

    <TextView android:id="@+id/lblDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="@string/PhotoDesc"    
      android:layout_below="@id/imgView" /> 

    <EditText android:id="@+id/edtDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"       
      android:layout_below="@id/lblDesc" /> 

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="horizontal" 
       android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="@string/TakePhotos" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="@string/Upload" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
       android:gravity="center_vertical|center_horizontal"    
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="@string/Setup"    
       android:layout_weight="1" /> 
    </LinearLayout>      

</RelativeLayout> 

답변

4

보인다.

layout_weight with LinearLayout은 종종 오해됩니다. LinearLayout은 두 번에 걸쳐 자녀를 측정합니다. 먼저, layout_width 또는 layout_height를 기준으로 어린이를 측정 한 다음 모든 어린이를 측정 한 후에 남아있는 여유 공간을 가중치로 나눕니다.

이 두 가지 중요한 일을 의미

  • 당신은 (이전 버전 또는 fill_parent) match_parent 말을하고 싶지는 않을 레이아웃의 방향으로 무게를. (layout_width="match_parent"LinearLayout이고 orientation="horizontal" 또는 layout_height="match_parent"orientation="vertical"입니다.) match_parent가 먼저 적용되어 현재 사용 가능한 모든 공간을 소비하고 나머지 아이들은 남겨 두지 않습니다.
  • 가중치가있는 여러보기간에 공간을 균등하게 나누거나 실제 내용 크기와 상관없이 하나의보기를 남겨 두려는 경우 0ydip을 ​​layout_width 또는 layout_height 값으로 사용하십시오.

이 레이아웃을 시도해보십시오

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

    <ImageView android:id="@+id/imgView" 
      android:layout_width="match_parent" 
      android:layout_height="0dip"    
      android:layout_weight="1" /> 

    <TextView android:id="@+id/lblDesc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/PhotoDesc"    
     android:layout_below="@id/imgView" /> 

    <EditText android:id="@+id/edtDesc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"       
     android:layout_below="@id/lblDesc" /> 

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
      android:gravity="center_vertical|center_horizontal" 
      android:layout_width="0dip"    
      android:layout_height="wrap_content" 
      android:layout_weight="1"    
      android:text="@string/TakePhotos" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
      android:gravity="center_vertical|center_horizontal" 
      android:layout_width="0dip"    
      android:layout_height="wrap_content" 
      android:layout_weight="1"    
      android:text="@string/Upload" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
      android:gravity="center_vertical|center_horizontal"    
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:text="@string/Setup"    
      android:layout_weight="1" /> 
    </LinearLayout>      

</LinearLayout> 
+0

굉장한 답변 2 회! 둘 다 맞지만 레이아웃 간격이 어떻게 나누어 지는지에 대한 간단한 설명이 매우 유용했습니다. 나는 안드로이드 개발의 날 2에 있고 나는 그것을 얻지 못하고 있었다! 감사! – Paul

+1

두 가지 추가 세부 정보로 답을 혼란스럽게하고 싶지는 않았지만 추가 정보가 마음에 들었으므로 여기에 또 다른 흥미로운 팁이 있습니다. LinearLayout의 자식과 함께 0dip/weight 기술을 사용하면 해당 자식에 대한 초기 측정 패스를 건너 뛰고 중량 측정 통과에만 의존합니다. (체중이없는 어린이들도 한 번만의 패스에 의존합니다.) 자녀가 실제로 측정 시간이 걸리는 복잡한 하위 계층 인 경우 중요한 최적화가 될 수 있습니다. – adamp

3
다음, android:orientation="vertical"으로 RelativeLayout LinearLayout로 변경하여 이미지 뷰에 android:layout_weight="1"를 추가

.

아래의 예제 코드. ImageView의 배경색을 빨간색으로 설정하여 효과를 확인합니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       android:orientation="vertical"> 

    <ImageView android:id="@+id/imgView" 
       android:layout_width="fill_parent" 
       android:layout_height="0dp"    
       android:layout_alignParentTop="true" 
       android:layout_weight="1" 
       android:background="#FF0000" /> 

    <TextView android:id="@+id/lblDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:text="test" /> 

    <EditText android:id="@+id/edtDesc" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 

    <!-- Bottom layout contains the three buttons - Take Photos, Transmit, and Setup. --> 
    <LinearLayout android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:orientation="horizontal" 
       android:weightSum="3"> 

     <!-- Take photos button --> 
     <Button android:id="@+id/btnCamera" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="btn1" /> 

     <!-- Transmit button --> 
     <Button android:id="@+id/btnUpload" 
       android:gravity="center_vertical|center_horizontal" 
       android:layout_width="fill_parent"    
       android:layout_height="wrap_content" 
       android:layout_weight="1"    
       android:text="btn2" />    

     <!-- Setup button --> 
     <Button android:id="@+id/btnSetup" 
       android:gravity="center_vertical|center_horizontal"    
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:text="btn3"    
       android:layout_weight="1" /> 
    </LinearLayout>      

</LinearLayout>