ImageView不会填充父级

时间:2012-07-16 19:01:11

标签: android scrollview relativelayout

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

像这样...

<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强制其尺寸为图像文件的大小。它不会垂直拉伸成RelativeLayout的高度。我也试过“match_parent”无济于事。图像是一个9补丁。

想法?

4 个答案:

答案 0 :(得分:20)

将可绘制内容应用为ImageView的来源有点带有一个固有的要求,即您希望视图尽其所能来容纳内容,而无需非常修改内容本身。通常,这是您想要的ImageView行为。

您真正想要的是通过将可绘制内容设置为视图的background而获得的行为,您根本不需要ImageView。背景设计为简单地拉伸,填充等等视图的大小。此外,由于您使用的是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>

答案 1 :(得分:8)

试试这个

<ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:background="@drawable/ic_launcher"
                android:scaleType="fitXY"
                android:layout_alignParentRight="true"
                />

这是我得到的

enter image description here

和代码ID

<?xml version="1.0" encoding="utf-8"?>

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    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:layout_alignParentRight="true"
            android:background="@drawable/ic_launcher"
            android:scaleType="fitXY" />
    </RelativeLayout>
</ScrollView>

答案 2 :(得分:1)

您的问题与ImageView或9补丁本身无关,而是与您在ScrollView中包装所有内容的事实无关。 ScrollView将自动强制其 children 直接子项包装其内容,无论您是将其告诉FILL_PARENT还是MATCH_PARENT - 两者都完全相同顺便说说;唯一的区别是名称,它更好地反映了旗帜的实际行为。

幸运的是ScrollView提供了一种方法来强制它用标志填充视口,这将使行为非常类似于将FILL_PARENT设置为常规视图。添加属性android:fillViewport或使用代码中的setFillViewport()

修改:为了清楚起见,您需要在ScrollView上设置该标记。另外,如果它是应该有阴影的ScrollView,你能不能将你的9补丁作为背景发送给它?我想这取决于你的实际图像是什么样的。关于你评论:是的,RelativeLayout在定位和调整子项大小方面是灵活的,但任何孩子仍然会受到其父级的约束。

我确实感觉我们中的某些人可能正在努力寻找与你的想法不同的东西。用简单的图纸来澄清事情肯定会有所帮助。

答案 3 :(得分:0)

你想在你的图像右边有一个阴影,然后使用Horizontal Orientation的单一布局,你决定使用相对布局是好的。使用

android:orientation="vertical"

在此布局中,添加这两个图像。如果你还有疑问,请给我这两张图片或样本图片,我会给你代码