Android中ImageView的最大宽度和高度
所以我有一个 ImageView 设置为
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"
此图像视图用于显示从图库或相机获取的图片。在这两种情况下,图像都不会调整大小以适合图像视图,它只是根据需要拉伸其空间。
知道如何让它保持在这些范围内吗?
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/txtDescription"
android:layout_below="@+id/txtSubject"
android:inputType="textMultiLine"
android:height="80px"
android:hint="@string/description"></EditText>
<EditText
android:layout_height="wrap_content"
android:layout_below="@+id/txtDescription"
android:layout_width="fill_parent"
android:id="@+id/txtMorada"
android:hint="@string/address" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/txtMorada"
android:id="@+id/btGPS"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_menu_compass"></ImageButton>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/btGPS"
android:layout_marginTop="25px"
android:id="@+id/imgPoint"
android:src="@drawable/google_logo_small"
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"></ImageView>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/imgPoint"
android:id="@+id/btGallery"
android:layout_below="@+id/btCamera"
android:src="@drawable/ic_menu_gallery"
android:layout_alignParentRight="true"></ImageButton>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10px"
android:id="@+id/btSubmit"
android:layout_below="@+id/btGallery"
android:text="@string/submit"></Button>
<ImageButton
android:layout_height="wrap_content"
android:id="@+id/btMap"
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_menu_mapmode"></ImageButton>
<TextView
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLatitude"
android:text="Latitude"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLongitude"
android:layout_below="@+id/lblNewPointLatitude"
android:text="Longitude"></TextView>
<ImageButton
android:layout_below="@+id/btMap"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+is/imgPoint"
android:id="@+id/btCamera"
android:layout_marginTop="25px"
android:src="@drawable/ic_menu_camera"
android:layout_alignParentRight="true"></ImageButton>
<Spinner
android:layout_height="wrap_content"
android:id="@+id/spCategoria"
android:layout_width="fill_parent"
android:prompt="@string/spCategoriaPrompt"></Spinner>
<Spinner
android:layout_height="wrap_content"
android:id="@+id/spSubcategoria"
android:layout_below="@+id/spCategoria"
android:layout_width="fill_parent"
android:prompt="@string/spSubcategoriaPrompt"></Spinner>
<EditText
android:layout_height="wrap_content"
android:layout_below="@+id/spSubcategoria"
android:layout_width="fill_parent"
android:id="@+id/txtSubject"
android:hint="@string/subject"></EditText>
</RelativeLayout>
</ScrollView>
So I have an ImageView set with
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"
This image view is used to show a picture that is obtained from the gallery or camera. In both cases, the image is not resized to fit inside the imageview, it just stretches its space as much as it needs.
Any idea how to make it stay inside those bounds?
<?xml version="1.0" encoding="utf-8"?>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/txtDescription"
android:layout_below="@+id/txtSubject"
android:inputType="textMultiLine"
android:height="80px"
android:hint="@string/description"></EditText>
<EditText
android:layout_height="wrap_content"
android:layout_below="@+id/txtDescription"
android:layout_width="fill_parent"
android:id="@+id/txtMorada"
android:hint="@string/address" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/txtMorada"
android:id="@+id/btGPS"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_menu_compass"></ImageButton>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/btGPS"
android:layout_marginTop="25px"
android:id="@+id/imgPoint"
android:src="@drawable/google_logo_small"
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"></ImageView>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/imgPoint"
android:id="@+id/btGallery"
android:layout_below="@+id/btCamera"
android:src="@drawable/ic_menu_gallery"
android:layout_alignParentRight="true"></ImageButton>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10px"
android:id="@+id/btSubmit"
android:layout_below="@+id/btGallery"
android:text="@string/submit"></Button>
<ImageButton
android:layout_height="wrap_content"
android:id="@+id/btMap"
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_menu_mapmode"></ImageButton>
<TextView
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLatitude"
android:text="Latitude"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLongitude"
android:layout_below="@+id/lblNewPointLatitude"
android:text="Longitude"></TextView>
<ImageButton
android:layout_below="@+id/btMap"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+is/imgPoint"
android:id="@+id/btCamera"
android:layout_marginTop="25px"
android:src="@drawable/ic_menu_camera"
android:layout_alignParentRight="true"></ImageButton>
<Spinner
android:layout_height="wrap_content"
android:id="@+id/spCategoria"
android:layout_width="fill_parent"
android:prompt="@string/spCategoriaPrompt"></Spinner>
<Spinner
android:layout_height="wrap_content"
android:id="@+id/spSubcategoria"
android:layout_below="@+id/spCategoria"
android:layout_width="fill_parent"
android:prompt="@string/spSubcategoriaPrompt"></Spinner>
<EditText
android:layout_height="wrap_content"
android:layout_below="@+id/spSubcategoria"
android:layout_width="fill_parent"
android:id="@+id/txtSubject"
android:hint="@string/subject"></EditText>
</RelativeLayout>
</ScrollView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
adjustViewBounds
属性:Try the
adjustViewBounds
attribute:除了为宽度和高度设置明确的值之外,您还将
layout_width
和layout_height
设置为wrap_content
。相反,您应该将layout_width和layout_height设置为某个数值。另外,使用 dp 而不是 px。请参阅支持多种屏幕尺寸。you have both
layout_width
andlayout_height
set towrap_content
, in addition to setting explicit values for width and height. Instead, you should just set layout_width and layout_height to some number value. Also, use dp instead of px. See supporting multiple screen sizes.您尝试过 android:scaleType="fitCenter" 吗?此外,正如 Mayra 所指出的,视图的尺寸不应设置为
wrap_content
have you tried android:scaleType="fitCenter"? Also the dimensions of the view should not be set to
wrap_content
as pointed out by Mayra尝试包含以下内容:
xmlns:android="http://schemas.android.com/apk/res/android"
例子 :
Try including this:
xmlns:android="http://schemas.android.com/apk/res/android"
Example :
虽然已经提到了所有答案,但需要添加
它,还应注意如果使用
android:background
来设置图像,则缩放不起作用。要允许缩放,请使用src
或srcCompat
等While all the answers already mentioned, it is required to add
it should be also noted that scaling does not work if
android:background
is used to set the image. To allow scaling make use ofsrc
orsrcCompat
like