安卓中ImageView的最大宽度和高度是多少?

79

所以我有一个设置了

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>

1
ImageView周围的布局是什么?你能发布一个更大的XML代码片段吗? - Cheryl Simon
1
你应该在XML中加入缩进和换行,这样会更易读。我注意到一个问题是在btCamera TextView中,你的layout_toRightOf写成了@+is而不是@+id。当然,这不是你的主要问题,但它会影响你的布局。 - Kevin Coppock
可能是重复的问题:Android ImageView 不遵守 maxWidth? - AZ_
5个回答

225

7

7

你同时将layout_widthlayout_height都设置为wrap_content,并且还为宽度和高度设置了明确的值。相反,你应该只将layout_widthlayout_height设置为某个数字值。另外,使用dp而不是px。请参见支持多个屏幕尺寸


在此上下文中,您不能将layout_width/height设置为整数值。 - Alex

2

尝试包含这个:xmlns:android="http://schemas.android.com/apk/res/android" 示例:

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="400dp"
    android:adjustViewBounds="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:src="@drawable/background_img"
    />

0

虽然所有的答案都已经提到了,但还需要补充一点。

android:adjustViewBounds="true"

需要注意的是,如果使用android:background来设置图像,则缩放不起作用。要允许缩放,请使用srcsrcCompat,例如:

<!-- Will not work -->
android:background="@drawable/my_image"

<!-- Works -->
android:src="@drawable/my_image"
<!-- Works -->
app:srcCompat="@drawable/my_image"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接