带有 ImageView 的Android ScrollView 无法正常工作

标签 android imageview scrollview

我有一个 ScrollView 和一个 ImageView 。它看起来像这样:

<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);

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

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

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

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

最佳答案

首先使用NestedScrollView作为您的 ScrollView 。其次试试这个:

<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>

希望对您有所帮助。

关于带有 ImageView 的Android ScrollView 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54186426/

相关文章:

java - RecyclerView.ViewHolder 中的 onClickListener

java - Gmail api 范围和格式不匹配

java - 抽屉导航 RTL

android - 将 ImageView 放在另一个前面?

ios - UIScrollView 取消 UIPageViewController 滚动时的手势

Android 设备监视器在 Mac OS X 上卡住

android - ImageView 很慢

android - ImageView 是动态宽度的正方形?

ios - 在之后/之前显示当前页码

android - 如何在Android Lollipop 中添加scrollview 边缘颜色效果?