【问题标题】:Making views the same height in LinearLayout when they are all wrap_content当它们都是 wrap_content 时,在 LinearLayout 中使视图具有相同的高度
【发布时间】:2013-05-09 02:02:35
【问题描述】:

我有一个水平线性布局,里面有三个视图。每个都将其 layout_width 设置为 0dp,并设置不同的权重。它们都为 layout_height 设置了 wrap_content。

我遇到的问题是,当其中一个包裹时,其他的不再填满 LinearLayout 的整个高度(它们有背景,所以看起来很难看)。

我想让它们全部填充 LinearLayout 中剩余的垂直空间,同时还允许它们在需要时包装内容。基本上,我希望他们都拥有最高兄弟姐妹的身高。

我尝试将 layout_gravity 设置为“fill”和“fill_vertical”,但没有任何效果。

这是一个布局示例:

<LinearLayout
            android:id="@+id/LinearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/background_dark"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/job"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="fill"
                android:layout_margin="1dp"
                android:layout_weight="4"
                android:background="@drawable/btn_bkgnd_dark_grey"
                android:drawableLeft="@drawable/icon_plus"
                android:onClick="jobPressed"
                android:padding="5dp"
                android:text="Add to Job fgh dfhfg "
                android:textAppearance="@style/rowitem_notes"
                android:textColor="@android:color/white" />

            <ImageButton
                android:id="@+id/bookmark"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="fill"
                android:layout_margin="1dp"
                android:layout_weight="2"
                android:background="@drawable/btn_bkgnd_dark_grey"
                android:onClick="bookmarkPressed"
                android:padding="5dp"
                android:src="@drawable/icon_bookmark" />

            <Button
                android:id="@+id/itemDetailsButton"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="fill"
                android:layout_margin="1dp"
                android:layout_weight="4"
                android:background="@drawable/btn_bkgnd_dark_grey"
                android:drawableRight="@drawable/icon_info"
                android:onClick="itemDetailsPressed"
                android:padding="5dp"
                android:text="Item Details"
                android:textAppearance="@style/rowitem_notes"
                android:textColor="@android:color/white" />

        </LinearLayout>

【问题讨论】:

标签: android android-layout android-linearlayout


【解决方案1】:

我已经更正了您的布局。使用 fill_parent 而不是包装子视图的内容。我使用了默认的 android 可绘制对象和颜色,将其替换为您的可绘制对象和样式。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
style="@android:style/ButtonBar"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
    android:id="@+id/job"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    android:layout_weight="4"
    android:background="#33B5E5"
    android:drawableLeft="@android:drawable/btn_star"
    android:onClick="jobPressed"
    android:padding="5dp"
    android:text="Add to Job fgh"
    android:textColor="@android:color/white"
    android:textSize="10sp" />

<ImageButton
    android:id="@+id/bookmark"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    android:layout_weight="1"
    android:background="#33B5E5"
    android:onClick="bookmarkPressed"
    android:padding="5dp"
    android:scaleType="fitCenter"
    android:src="@android:drawable/ic_input_add" />

<Button
    android:id="@+id/itemDetailsButton"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:layout_margin="2dp"
    android:layout_weight="4"
    android:background="#33B5E5"
    android:drawableRight="@android:drawable/ic_delete"
    android:onClick="itemDetailsPressed"
    android:padding="5dp"
    android:text="Item Details"
    android:textColor="@android:color/white"
    android:textSize="10sp" />

</LinearLayout>

【讨论】:

  • 谢谢。我使用的容器布局正在阻止视图正确调整大小(它是自定义布局),这就是为什么这不起作用。换行的 TextView 不会调整大小以匹配其内容,而是将其切断。不过,您的解决方案似乎在一般情况下有效。
  • 是和否。这是由于我的自定义布局测量代码中的错误,因此与原始问题无关。你的答案是正确的。
【解决方案2】:

您说以下内容:“基本上,我希望他们都拥有最高兄弟姐妹的身高”。 鉴于您不知道哪个兄弟姐妹的身高最高,那么您必须以编程方式进行,基本上是这样的:

  1. 首先获取每个小部件的高度,如下所示:

    int height = widget.getHeight();  //with this you know which is the tallest one
    
  2. 然后,创建一个 RelativeLayout 参数,以便将 layout_alignTop 属性设置为必须相应缩放到最高视图的视图。显然 align_Top 指的是最高视图的 id

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    layout.setLayoutParams(params);         
    params.addRule(RelativeLayout.ALIGN_TOP,R.id.tallest_widget);
    widget.setLayoutParams(params);
    
  3. 仅此而已,但请注意,在此解决方案中,您必须使用 RelativeLayout 而不是 LinearLayout。

我认为RelativeLayout 是解决此问题的方法,因为它为您提供了灵活性,在这种情况下,它允许您将视图的顶部与参考(即:最高的视图)相应地对齐。

【讨论】:

  • 我希望找到一种使用 xml 的方法。当我不得不退回到像这样简单的事情的代码时,这总是感觉像是 API 的失败。不过谢谢你,我可能必须按照你说的那样实现自定义布局。
【解决方案3】:

在这种情况下,我认为您应该这样做: 起初,所有按钮都有背景,所以我认为您应该知道所有视图的高度。也许你可以这样做,给 LinearyLayout maxHeight/minHeight 或固定值,然后在子视图中使用 layout_height="match_parent"。除非您必须在代码中务实地调整 Ui。

【讨论】:

  • 这个问题是在较小宽度的屏幕上,文本行换行并被切断。
猜你喜欢
  • 1970-01-01
  • 2011-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2021-08-13
相关资源
最近更新 更多