Android 带有图像视图的滚动视图无法正常工作

Android scrollview with imageview not working

我有一个滚动视图和一个图像视图。它看起来像这样:

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

            <ImageView
                android:id="@+id/theImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop" />

        </ScrollView>

然后我像这样动态设置图像背景: 位图 bmp;

String fileName = Utils.PATH + "/" + getIntent().getStringExtra(Utils.FILE_NAME_ON_INTENT);
bmp = Utils.decodeFile(this, fileName);
theImage.setImageBitmap(bmp);

所以我在滚动视图中看到了图像,但由于某种原因它没有滚动。 如果我像这样放置图像背景:

int img = R.drawable.some_image;
        theImage.setImageResource(img);

然后就可以了。但我这样做只是为了测试,我所有的图像都不在 drawable 中,我不知道如何以动态方式从 drawable 中获取图像。

有没有办法让它随着设置为位图 wap 滚动?

首先使用 NestedScrollView 作为滚动视图。其次试试这个:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

            <ImageView
                android:id="@+id/theImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop" />

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"/>

        </LinearLayout>

    </NestedScrollView>
</FrameLayout>

希望对您有所帮助。