2011-02-27 14 views
2

私の人生にとってはこれを理解できません。 (すべての利用可能なスペースを埋める)Androidレイアウトの問題

ImageViewの(等間隔) のTextView(必要に応じて垂直方向のスペースを取って) のEditText(必要に応じて垂直方向のスペースを取って) ボタンボタンボタン

:私は、次のようなレイアウトを(縦に)持ってしようとしています

次のレイアウト設定があります。イメージビューは現在200dipに設定されています。利用可能なすべてのスペースを埋めるためには、この価値はどうあるべきですか?他のコンポーネントが最初に来て、画像ビューは残っているものを取得する必要があります。私はこれについての例を探し、何も見つけていない。

<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

あなたが必要とするすべての残りのスペースを取るためにImageViewに適切な重みを持つLinearLayoutであるように見えます。

layout_weight with LinearLayoutはよく誤解されます。 LinearLayoutは2回のパスで子供を測定します。まず、指定されたlayout_widthまたはlayout_heightに基づいて子供を測定し、すべての子供が測定された後に残っている余分なスペースをその重みに従って分割します。

これは、2つの重要な事柄を意味:

  • あなたは(古いバージョンやfill_parent)match_parentを言いたいことはありませんレイアウトの方向に重みを持つが。 (のLinearLayoutの場合、orientation="horizontal"またはの場合はorientation="vertical")match_parentが最初に有効になり、現在利用可能なすべての領域が消費され、残りの子は残りません。
  • 重複した複数のビュー間で均等にスペースを分割したい場合や、実際のコンテンツサイズに関係なく1つのビューを消費するスペースを消費する場合は、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つの答え!どちらも正しいですが、レイアウトの間隔がどのように分割されているかについての簡単な説明は非常に役に立ちました。私はAndroid開発の2日目にいます。私はそれを手に入れていませんでした!ありがとう! – Paul

+1

私は2つの余分な詳細で答えを混乱させたくありませんでしたが、余分な情報が好きだったので、もう1つの興味深いヒントがあります。 LinearLayoutの子で0ディップ/ウェイトテクニックを使用すると、その子の初期測定パスはスキップされ、ウェイト測定パスにのみ依存します。 (体重のない子供は、1回のパスだけに依存します)。子供が実際に測定に時間がかかる複雑なサブ階層である場合、これは重要な最適化になります。 – adamp

3
は、あなたのImageViewのに android:layout_weight="1"を追加し、 android:orientation="vertical"LinearLayoutにご RelativeLayoutを変更し

下記のコード例。私は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> 
関連する問題