为什么添加ScrollView后ImageView图像会缩放?

时间:2020-05-21 07:01:36

标签: android android-layout android-imageview android-cardview android-scrollview

我想滚动我的名片视图,但是当我添加ScrollView时,ImageView中的图像会被缩放/拉伸。以下屏幕截图代表了我的问题。

ImageView before adding ScrollView

ImageView after adding Scrollview

我的Activity.xml

spring.jpa.open-in-view=false

如上面的代码所示。我也将fillViewport添加为true,但仍然无法正常工作。当我删除滚动视图但无法滚动时,CardView可以正常工作。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

将ImageView scaleType从centerCrop更改为centerInside

答案 1 :(得分:0)

您可以将android:layout_gravity="center"用于包含imageview的线性布局,并将比例类型更改为centerInsidefitCenter

<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.cardview.widget.CardView
        android:layout_margin="10dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:fillViewport="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:padding="10dp"
                android:layout_gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_marginTop="10dp"
                    android:id="@+id/orbitPostSharedImg"
                    android:layout_gravity="center_horizontal"
                    android:scaleType="centerInside"
                    android:src="@drawable/rabindranath_tagore"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>

            </LinearLayout>
        </ScrollView>
    </androidx.cardview.widget.CardView>
</LinearLayout>