安卓布局问题

发布于 2024-10-19 09:40:01 字数 2666 浏览 5 评论 0原文

我一生都无法弄清楚这一点。我试图拥有以下布局(垂直):

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-10-26 09:40:01

看起来您所需要的只是一个在 ImageView 上具有适当权重的 LinearLayout 来占用剩余空间。

带有LinearLayout的layout_weight经常被误解。 LinearLayout 分两次测量其子级。首先,根据给定的layout_width或layout_height测量子项,然后在测量完所有子项后,剩余的剩余空间根据其权重进行划分。

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

  • 您永远不想在布局方向上使用权重的 match_parent (或旧版本上的 fill_parent )。 (即 layout_width="match_parent" 对于带有 orientation="horizo​​ntal"layout_height="match_parent"LinearLayout对于 orientation="vertical"。) match_parent 将首先生效,消耗所有当前可用空间,并且不为剩余的子项留下任何剩余空间。
  • 当您想要在多个具有权重的视图之间平均分配空间或让一个视图消耗剩余空间而不考虑实际内容大小时,请使用 0dip 作为 layout_widthlayout_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>

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>
她说她爱他 2024-10-26 09:40:01

RelativeLayout 更改为带有 android:orientation="vertical"LinearLayout,然后添加 android:layout_weight="1"代码>到你的ImageView。

下面的示例代码。我将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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文