安卓ScrollView在子ImageView顶部和底部添加额外的填充

19

我在渲染ScrollView方面遇到了问题。实际上,布局是固定的页眉和页脚,中间有一个ScrollView。我的想法是当内容EditText激活屏幕键盘时,如果图像的高度超过了中间视图,那么图像可以滚动。

在测试布局时,我发现在我的Android手机(Xperia X10 Android 1.6)上,ImageView的顶部和底部都添加了大量的填充。

我希望能够得到任何建议,以防止这种情况发生。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- Header -->
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">
        <TextView
            android:text="Share your photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left"/>
        <Button
            android:id="@+id/btnStack"
            android:text="Stacks"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"/>
    </RelativeLayout>
    <!-- End Header -->


    <!-- Body -->
    <ScrollView
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="5px"
        android:background="#CCCCCC">
        <ImageView
            android:id="@+id/imgPreview"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
    </ScrollView>
    <!-- End Body -->


    <!-- Footer -->
    <RelativeLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_alignParentBottom="true">
        <EditText
            android:id="@+id/caption"
            android:text="Type your caption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/caption"
            android:background="#CCCCCC">
            <Button
                android:id="@+id/btnUpload"
                android:text="Share"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
            <Button
                android:id="@+id/btnCancel"
                android:text="Cancel"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

    </RelativeLayout>
    <!-- End Footer -->

</LinearLayout>
4个回答

37

经过一些实验后,我发现将以下语法添加到我的Java代码中解决了问题:

    imgPreview.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    imgPreview.setAdjustViewBounds(true);

2
为什么要这样做? - Joshua Pinter
我在ScrollView内的RelativeLayout上遇到了这个问题。在我的xml上放置adjustViewBounds并不能解决它。这是什么原因呢?我注意到顶部添加的padding是状态栏的高度,底部的padding与导航栏的高度相同。 - John Ernest Guadalupe
帮了我很大的忙。谢谢。 - Gereltod
@JohnErnestGuadalupe - 将ScrollView的高度设置为0dp。 - Alaa M.

28

只需在你的ImageView XML描述中添加以下属性即可完成该任务:

android:adjustViewBounds="true"

1
这对我处理一个GridLayout很有帮助,其中ImageView子元素具有额外的顶部和底部填充。 - Satisfakshin

14
android:fillViewport="true"
android:fadingEdge="none"

在scrollview中使用这些属性。


嗨,我试过了,但是好像都没有什么区别。我们说的不是几个像素,而是几百个。即使我指定了ImageView的像素宽度和高度,它仍然不能正确地工作。 - Bobby Sciacchitano
最佳答案!fillViewport可以完成任务,并允许子布局占用所请求的空间。 - sud007

2
您可以像这样为子元素设置marginTop和bottom为负数。
<HorizontalScrollView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#ffffff"
        android:overScrollMode="never"
        android:scrollbars="none">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginBottom="-10dp"
            android:layout_marginTop="-10dp">


    </LinearLayout>

</HorizontalScrollView>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接