【问题标题】:Proper way to divide into 4 pieces an android layout将android布局分成4块的正确方法
【发布时间】:2012-09-15 08:37:39
【问题描述】:

我想要实现的是,划分下图中描述的行布局。我应该怎么做才能将行分成3个完全相同的大小和1个未知的大小? X 大小相同,我不知道也不想指定是否不需要...

编辑:按钮位于左侧、中间和右侧。

【问题讨论】:

    标签: android listview layout row android-relativelayout


    【解决方案1】:

    您的最左侧尺寸是否有最小宽度? 如果是这样,您应该使用水平方向的 LinearLayout。 它可以包含 2 个线性布局,一个包含 3 个视图(您的按钮),宽度为 0,每个权重为 1,另一个线性布局具有 minimumWidth 集。 您可以为第一个布局指定宽度,而不是 marginRight。

    Ted Hopp 做对了 ;)

    【讨论】:

    • 这行不通。第一个内部LinearLayout 不会扩展以填充可用空间。也不需要嵌套LinearLayouts;所有四个按钮都可以放在一个 LinearLayout 中。
    • 我放了第二个 LinearLayout 因为我没有发现他想要 4 个按钮 ;)
    • 不尝试你的答案,是吗?可以是wrap_content?,其实我想要的是?是包装内容宽度,其他填充可用空间。
    • @KaaN - 不是真的。这种方法需要知道第四个按钮的宽度,因为该宽度用于设置容器布局的右边距。
    • 在所有情况下,如果不指定至少一个宽度,我认为您无法做到您想要的。 (最后一个视图的 Wrap_content 应该是它,如果它是一个带有一些文本的按钮)
    【解决方案2】:

    您可以使用 layout_weight 属性。为所有 x 布局赋予相同的权重,为问号赋予不同的权重,直到屏幕按照您的喜好划分

    【讨论】:

      【解决方案3】:

      您可以使用android:layout_weight 按比例分配额外空间。您希望左侧的三个按钮吸收所有额外的宽度,因此右侧(第四个)按钮的默认权重应为 0。由于您还希望它们具有相同的宽度,最简单的方法是为它们分配 0dp 的宽度和赋予它们相同的权重:

      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal" >
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1" />
      
          <Button
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1" />
      
      
          <Button
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        RelativeLayout 中使用LinearLayout。将 3 个项目放入 LinearLayout 并给它们相同的 weight。在RelativeLayout的帮助下,将未知项目放在LinearLayout的右侧。

        左边的元素会根据右边的宽度对齐自己。 这是代码:https://gist.github.com/3772838

        这里有两个不同大小的最右边元素的屏幕截图:

        http://goo.gl/Nezmn

        http://goo.gl/XbQwL

        科莱凝胶 =)

        【讨论】:

          【解决方案5】:
          <?xml version="1.0" encoding="utf-8"?>
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical" >
          
          <RelativeLayout
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" android:id="@+id/rlayoutParent">
          
          
              <RelativeLayout
                  android:layout_width="wrap_content"
                  android:id="@+id/rlayoutButtons" android:layout_height="wrap_content">
          
                  <Button
                      android:id="@+id/button1"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Button" />
          
                  <Button
                      android:id="@+id/button2"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Button" android:layout_toRightOf="@+id/button1"/>
          
                  <Button
                      android:id="@+id/button3"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:text="Button" android:layout_toRightOf="@+id/button2"/>
          
              </RelativeLayout>
          
          
              <RelativeLayout
                  android:id="@+id/rlayoutOther"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_toRightOf="@+id/rlayoutButtons" android:gravity="right">
          
          
                  <Button
                      android:id="@+id/button4"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:text="Button" />
          
              </RelativeLayout>
          
          </RelativeLayout>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-07-03
            • 1970-01-01
            • 2017-11-24
            • 2018-07-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多