我有一个 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/