Android的布局问题 [英] Android layout issues

查看:112
本文介绍了Android的布局问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生命摸不着头脑。我试图具有以下布局(垂直):

ImageView的(填充所有可用空间) TextView中(以根据需要垂直空间) 的EditText(以根据需要垂直空间) 按钮按钮按钮(等距)

我有以下的布局设置。图像视图目前定为200dip。我应该将此值,以填补所有可用空间?其他部件应该先和图像视图应该得到什么遗留下来的。我已经寻找上一个这样的例子,并没有发现任何东西。

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
                机器人:layout_width =FILL_PARENT
                机器人:layout_height =FILL_PARENT>

   < ImageView的机器人:ID =@ + ID / imgView
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =200dip
              机器人:layout_alignParentTop =真/>

   < TextView的机器人:ID =@ + ID / lblDesc
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =WRAP_CONTENT
             机器人:文本=@字符串/ PhotoDesc
             机器人:layout_below =@ ID / imgView/>

   < EditText上机器人:ID =@ + ID / edtDesc
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =WRAP_CONTENT
             机器人:layout_below =@ ID / lblDesc/>

   <! - 底部布局包含三个按钮 - 拍摄照片,发送,和设置。 - >
   < LinearLayout中的android:layout_width =FILL_PARENT
                 机器人:layout_height =WRAP_CONTENT
                 机器人:layout_alignParentBottom =真
                 机器人:方向=横向
                 机器人:weightSum =3>

      <! - 拍照按钮 - >
      <按钮机器人:ID =@ + ID / btnCamera
              机器人:重力=center_vertical | center_horizo​​ntal
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =WRAP_CONTENT
              机器人:layout_weight =1
              机器人:文本=@字符串/ TakePhotos/>

      <! - 发射按钮 - >
      <按钮机器人:ID =@ + ID / btnUpload
              机器人:重力=center_vertical | center_horizo​​ntal
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =WRAP_CONTENT
              机器人:layout_weight =1
              机器人:文本=@字符串/上传/>

      <! - 设置按钮 - >
      <按钮机器人:ID =@ + ID / btnSetup
              机器人:重力=center_vertical | center_horizo​​ntal
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =WRAP_CONTENT
              机器人:文本=@字符串/设置
              机器人:layout_weight =1/>
   < / LinearLayout中>

< / RelativeLayout的>
 

解决方案

看起来像所有你需要的是一个的LinearLayout 上的 ImageView的适当的重量把剩余的空间。

layout_weight与的LinearLayout 经常被误解。 的LinearLayout 衡量两遍的孩子。首先,儿童是基于给定的layout_width或layout_height测得,那么所有的孩子之后已经测量遗留任何额外的空间是根据它们的权重分配。

这意味着两个重要的事情:

  • 您永远不想说match_parent(或FILL_PARENT在旧版本)与布局的方向的权重。 (即 layout_width =match_parent的LinearLayout 方向=横向 layout_height =match_parent方向=垂直)。match_parent生效第一,消耗所有目前可用的空间,并没有留下遗留下来的其余儿童。
  • 当你想平分空间中的多个视图之间体重还是有一种观点认为占用任何空间留给没有考虑到实际内容的大小,使用0dip作为 layout_width layout_height 值。

试试这个布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:方向=垂直>

   < ImageView的机器人:ID =@ + ID / imgView
          机器人:layout_width =match_parent
          机器人:layout_height =0dip
          机器人:layout_weight =1/>

   < TextView的机器人:ID =@ + ID / lblDesc
         机器人:layout_width =match_parent
         机器人:layout_height =WRAP_CONTENT
         机器人:文本=@字符串/ PhotoDesc
         机器人:layout_below =@ ID / imgView/>

   < EditText上机器人:ID =@ + ID / edtDesc
         机器人:layout_width =match_parent
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_below =@ ID / lblDesc/>

   <! - 底部布局包含三个按钮 - 拍摄照片,发送,和设置。 - >
   < LinearLayout中的android:layout_width =match_parent
             机器人:layout_height =WRAP_CONTENT
             机器人:方向=横向
             机器人:weightSum =3>

      <! - 拍照按钮 - >
      <按钮机器人:ID =@ + ID / btnCamera
          机器人:重力=center_vertical | center_horizo​​ntal
          机器人:layout_width =0dip
          机器人:layout_height =WRAP_CONTENT
          机器人:layout_weight =1
          机器人:文本=@字符串/ TakePhotos/>

      <! - 发射按钮 - >
      <按钮机器人:ID =@ + ID / btnUpload
          机器人:重力=center_vertical | center_horizo​​ntal
          机器人:layout_width =0dip
          机器人:layout_height =WRAP_CONTENT
          机器人:layout_weight =1
          机器人:文本=@字符串/上传/>

      <! - 设置按钮 - >
      <按钮机器人:ID =@ + ID / btnSetup
          机器人:重力=center_vertical | center_horizo​​ntal
          机器人:layout_width =0dip
          机器人:layout_height =WRAP_CONTENT
          机器人:文本=@字符串/设置
          机器人:layout_weight =1/>
   < / LinearLayout中>

< / LinearLayout中>
 

I can't for the life of me figure this out. I am attempting to have the following layout (vertically):

ImageView (filling all available space) TextView (taking vertical space as needed) EditText (taking vertical space as needed) Button Button Button (spaced equally)

I have the following layout setup. The image view is currently set at 200dip. What should this value be to fill all available space? The other components should come first and the image view should get what's left over. I have searched for an example on this and have not found anything yet.

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

解决方案

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>

这篇关于Android的布局问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆