gpt4 book ai didi

android - ImageView 不会填充父级

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:39 25 4
gpt4 key购买 nike

我的其中一个屏幕上有一个 ScrollView。我希望右边缘有阴影。我决定最简单的方法是将 ScrollView 的子项设为 RelativeLayout,并让 RelativeLayout 有两个子项——一个是 LinearLayout,它将容纳屏幕布局,第二个 View 是阴影。

像这样...

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>

<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/shadow"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</ScrollView>

不幸的是,这不太行得通。 ImageView 强制其尺寸为图像文件的大小。它不会垂直拉伸(stretch)为 RelativeLayout 的高度。我也尝试过“match_parent”但无济于事。该图像是一个 9 补丁。

想法?

最佳答案

将可绘制内容应用为 ImageView 的源在某种程度上带有一个内在要求,即您希望 View 尽其所能来容纳内容,而无需过多修改内容本身。通常,这是您希望 ImageView 的行为。

您真正想要的是通过将可绘制内容设置为 View 的 background 而获得的行为,您根本不需要 ImageView。背景设计用于简单地拉伸(stretch)、填充等以适应 View 的任何大小。此外,由于您使用的是 RelativeLayout,您可以通过添加 id 和一些额外的 layout_align参数。

        <RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/content_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- stuff -->
</LinearLayout>

<View
android:layout_width="11dp"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/content_layout"
android:layout_alignBottom="@id/content_layout"
android:background="@drawable/shadow"
/>
</RelativeLayout>

关于android - ImageView 不会填充父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11510804/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com