Android 当所有视图都是包裹内容时,使它们在LinearLayout中具有相同的高度
我有一个水平直线布局,里面有三个视图。每个都将其布局宽度设置为0dp,并设置了不同的权重。它们都为布局高度设置了包裹内容 我遇到的问题是,当其中一个包裹时,其他的不再填充线性布局的整个高度(它们有背景,所以看起来很难看) 我想让它们填满LinearLayout中剩余的垂直空间,同时也允许它们在需要时包装内容。基本上,我希望他们都有最高兄弟姐妹的身高 我曾尝试将布局重力设置为“填充”和“填充垂直”,但没有效果 以下是布局示例:Android 当所有视图都是包裹内容时,使它们在LinearLayout中具有相同的高度,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我有一个水平直线布局,里面有三个视图。每个都将其布局宽度设置为0dp,并设置了不同的权重。它们都为布局高度设置了包裹内容 我遇到的问题是,当其中一个包裹时,其他的不再填充线性布局的整个高度(它们有背景,所以看起来很难看) 我想让它们填满LinearLayout中剩余的垂直空间,同时也允许它们在需要时包装内容。基本上,我希望他们都有最高兄弟姐妹的身高 我曾尝试将布局重力设置为“填充”和“填充垂直”,但没有效果 以下是布局示例: <LinearLayout androi
<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>
在这种情况下,我认为您应该这样做: 首先,所有按钮都有背景,所以我认为您应该知道所有视图的高度。也许您可以这样做,为线性布局指定
maxHeight/minHeight
或固定值,然后在子视图中使用layout\u height=“match\u parent”
。除非您必须在代码中实用地调整Ui。您可以这样说:“基本上,我希望它们都具有最高兄弟姐妹的高度”。
考虑到你不知道哪个兄弟姐妹的身高最高,那么你必须通过编程来完成,基本上是这样的:
int height = widget.getHeight(); //with this you know which is the tallest one
int height = widget.getHeight(); //with this you know which is the tallest one
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);
我认为
RelativeLayout
是解决这个问题的方法,因为它给了您灵活性,在这种情况下,它允许您将视图的顶部与参考对齐(即:最高视图)。我已经更正了您的布局。对子视图使用“填充父视图”而不是“换行内容”。我已经使用了默认的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>
您是否尝试将布局容器中的所有视图更改为“匹配父视图”?如果没有,为什么不呢?我想如果设置layout\u height=“match\u parent”将导致容器(LinearLayout)占据整个屏幕。可能的问题是,在较小宽度的屏幕上,文本行会缠绕并被切断。我希望找到一种使用xml的方法。当我不得不回到像这样的简单事情的代码时,总是感觉像是API的失败。不过谢谢你,我可能需要实现一个定制的布局来完成你说的。谢谢。我使用的容器布局阻止视图正确调整大小(这是一个自定义布局),这就是为什么它不起作用。换行文本视图不会调整大小以匹配其内容,而是将其截断。您的解决方案似乎在一般情况下有效。是和否。这是由于我的自定义布局度量代码中的错误,因此与原始问题无关。你的回答是正确的。