android - 将 ImageView 添加到 Android 中的 ScrollView 时出现冗余边距

标签 android imageview scrollview

我一直在尝试在 600x800 的 AVD 上使用带有 JPG (~770 x 1024) 的单个 ImageView 上的 ScrollView 。

我的 main.xml 是:

<?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"
     >
<ScrollView 
android:id="@+id/scroller"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
/>
</LinearLayout>

现在,我添加一个带有

的 ImageView
setContentView(R.layout.main);
ScrollView sv = (ScrollView)findViewById( R.id.scroller );
ImageView iv = new ImageView(this);
iv.setImageDrawable( new BitmapDrawable( "/sdcard/770x1024.jpg" ) ); // same happens with ScaleDrawable.
iv.setScaleType( ScaleType.CENTER_INSIDE );
sv.addView( sv ); // and it does not go any better if I use Linear Layout between the ScrollView and the ImageView.

结果是 图像显示在 ScrollView 的中间,顶部和底部包裹着背景区域,如下所示:

     | } blank
     | }
Image|
.    |
.    :
.    :
     : } blank
     : }
     ^-scrollbar

不仅仅是

Image|
.    |
.    |
.    |
.    :

我尝试将 ImageView 的背景设置为红色,并验证了空白边距是 ImageView 背景。

iv.setBackgroundColor( color.Red );

我希望图像不超过它的大小(缩放到 AVD 大小)并且我希望 ScrollView 让我滚动剩余部分(如果有的话)。

出于某种原因,我看到可绘制对象的大小为 600x1024。

此外 我尝试添加一个带有虚拟 TextView 的 LinearLayout,例如线性布局是 ImageView 和 TextView 的父级,而 ScrollView 是 LinearLayout 的父级。

LinearLayout dummy = new LinearLayout( this );
dummy.addView(iv);
TextView someTextView = new TextView( this );
someTextView.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT ));
dummy.addView( someTextView );
sv.addView( dummy );

结果很奇特: 整个布局设置为无文本 TextView (19) 的高度。

______________________
| T I T L E    B A R |
+--------------------+
|         []<-image  |
|        height=19px |
.                    .
.                    .
+--------------------+ 

避免拉伸(stretch)图像对我来说很重要。我不能将 FIT_XY 用于 ScaleType。 实现可能滚动的页面显示的推荐方法是什么? 我是否必须使用普通布局手动执行此操作并在 GestureDetector.OnScroll 事件上滚动?

谢谢

什穆尔

P.S:另一个观察结果:将图像缩小到 600x780(按比例缩放)它确实可以正常工作。不幸的是,对我来说,使用一组按比例缩小的图像克隆是不可行的。

最佳答案

将此添加到 xml 中的 ImageView:

android:adjustViewBounds="true"

关于android - 将 ImageView 添加到 Android 中的 ScrollView 时出现冗余边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2995890/

相关文章:

android - Flutter 版本解决此特定应用程序上的失败错误

objective-c - 如何在 iOS 中的 Scrollview 中添加 AutoScroll?

android - 在 Android 应用程序中实现类似 Google map 的搜索栏

android - 无法在 Android 应用程序中 POST MultiPartEntity

Android listview图片加载问题

安卓 ListView

ios - 如何旋转蒙版图像?

android - ScrollView 中的 ListView 在 Android 上不滚动

ios - 如何将属性值 UIScrollView.content.contentInsetAdjustmentBehavior 从 AppDelegate 更改为从不?

android - Xamarin 两个 Activity ,每个 Activity 有一个按钮 : Second activities button onClick seems bound before the activity is live