Android layout issues

随声附和 提交于 2019-12-04 15:10:44
adamp

Looks like all you need is a LinearLayout with appropriate weight on the ImageView to take the leftover space.

layout_weight with LinearLayout is often misunderstood. LinearLayout measures its children in two passes. First, children are measured based on the given layout_width or layout_height, then after all children have been measured any extra space left over is divided according to their weights.

This means two important things:

  • You never want to say match_parent (or fill_parent on older versions) with a weight in the direction of the layout. (i.e. layout_width="match_parent" for a LinearLayout with orientation="horizontal" or layout_height="match_parent" for orientation="vertical".) match_parent will take effect first, consuming all currently available space and leaving none left over for the remaining children.
  • When you want to divide space equally between several views with weight or have one view consume whatever space is left without regard to the actual content size, use 0dip as the layout_width or layout_height value.

Try this layout:

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

Change your RelativeLayout to a LinearLayout with android:orientation="vertical", then add android:layout_weight="1" to your ImageView.

Example code below. I set the background of the ImageView to the color red to see the effect.

<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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!