ImageView 未正确居中缩小的图像
我无法在 ImageView 中正确缩放图像并居中。
放大时没有问题,但是缩小时图像不再居中,并且 ImageView 的边界错误,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/stereomood_splash"
android:scaleType="fitCenter"
android:id="@+id/imageView1"
android:layout_width="100dip"
android:layout_height="200dip"></ImageView>
</LinearLayout>
这是一个错误吗?有人找到解决方法吗?
I'm having trouble getting an image to scale and center properly in an ImageView.
When up-scaling, there are no problems, but when down-scaling, the image no longer appears to be centred, and the bounds of the ImageView are wrong, like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/stereomood_splash"
android:scaleType="fitCenter"
android:id="@+id/imageView1"
android:layout_width="100dip"
android:layout_height="200dip"></ImageView>
</LinearLayout>
Is this a bug, and has anyone found a workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将
android:adjustViewBounds="true"
设置为 这个答案。您还需要将
android:layout_width
或android:layout_height
设置为wrap_content
,否则我相信您的硬编码边界将覆盖任何视图边界调整。You should set
android:adjustViewBounds="true"
as in this answer.You also need to set either
android:layout_width
orandroid:layout_height
to bewrap_content
, otherwise I believe your hard-coded bounds will override any view bounds adjustments.编辑:将其放在这里,因为它与遇到相同症状的人相关。
此问题仅存在于 XML 编辑器中 - 在真实手机或模拟器上,图像的大小和位置正确。根据特定图像和
ImageView
尺寸,编辑器预览将显示未正确调整大小和/或超出视图范围的图像。Edit: Put this here as it's relevant to those encountering the same symptoms.
This problem is only present in the XML editor - on a real phone or emulator the image is sized and positioned correctly. Depending on the particular image and
ImageView
dimensions, the editor preview will show the image incorrectly resized and/or out of the view bounds.