高度设置为 fill_parent 的 RelativeLayout 中的 ImageView 实例将像 RelativeLayout 一样拉伸。例如,如果同一布局中的 EditText 为 10 行高,则图像将拉伸到 10 行高。
如何在 FrameLayout 中复制这种行为?设置为 fill_parent 的图像高度似乎与 wrap_content 的行为相同,无论 FrameLayout 中其他项目的高度如何。
我宁愿纯粹在 XML 中处理这个问题。
高度设置为 fill_parent 的 RelativeLayout 中的 ImageView 实例将像 RelativeLayout 一样拉伸。例如,如果同一布局中的 EditText 为 10 行高,则图像将拉伸到 10 行高。
如何在 FrameLayout 中复制这种行为?设置为 fill_parent 的图像高度似乎与 wrap_content 的行为相同,无论 FrameLayout 中其他项目的高度如何。
我宁愿纯粹在 XML 中处理这个问题。
我发现了一个(可能是破解的)解决方法——将 FrameLayout 包装在 LinearLayout 中,让我将 FrameLayout 设置为 fill_parent,因此它的子级可以相应地拉伸。所以:
<LinearLayout
orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<FrameLayout
android:measureAllChildren="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:src="@drawable/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:layout_gravity="right|center_vertical" />
</FrameLayout>
</LinearLayout>
<RelativeLayout ... >
... other stuff ...
</RelativeLayout>
</LinearLayout>
ImageView 将拉伸到 EditText 的高度。
FrameLayout
它的所有儿童视图都覆盖在同一区域上。根据您的设置,您可以更改视图的显示方式。根据 Android 的FrameLayout,
框架布局的大小是其最大子项(加上填充)的大小,可见与否(如果 FrameLayout 的父项允许)。
此外,来自通用布局对象
FrameLayout 是最简单的布局对象类型。它基本上是屏幕上的一个空白区域,稍后您可以用单个对象填充它 - 例如,您将交换进出的图片。FrameLayout 的所有子元素都固定在屏幕的左上角;您不能为子视图指定不同的位置。随后的子视图将简单地绘制在先前的子视图上,部分或完全遮盖它们(除非较新的对象是透明的)。
在您的示例中, a 中的图像FrameLayout
不会被拉伸到其最大兄弟的尺寸,无论使用fill_parent
or wrap_content
。