【问题标题】:Maximum width and height for ImageView in AndroidAndroid中ImageView的最大宽度和高度
【发布时间】:2011-05-15 07:10:17
【问题描述】:

所以我用

设置了一个 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>

【问题讨论】:

  • ImageView 周围的布局是什么?你能发布一个更大的 xml sn-p 吗?
  • 您也应该在 XML 中添加带有缩进的换行符。它是不可读的。我注意到的一件事是在 btCamera TextView 中:您的 layout_toRightOf 说 @+is 而不是 @+id。当然,这不是您的主要问题,但它会影响您的布局。

标签: android resize imageview android-imageview stretch


【解决方案1】:

试试adjustViewBounds 属性:

android:adjustViewBounds="true"

【讨论】:

    【解决方案2】:

    除了为宽度和高度设置显式值之外,您还将layout_widthlayout_height 设置为wrap_content。相反,您应该将 layout_width 和 layout_height 设置为某个数值。另外,请使用 dp 而不是 px。见supporting multiple screen sizes

    【讨论】:

    • 在这种情况下,您不能将 layout_width/height 设置为整数值。
    【解决方案3】:

    你试过android:scaleType="fitCenter"吗?此外,不应将视图的尺寸设置为 wrap_content,正如 Mayra 所指出的那样

    【讨论】:

      【解决方案4】:

      尝试包括这个: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"
          />
      

      【讨论】:

        【解决方案5】:

        虽然上面已经提到了所有的答案,但需要补充

        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"
        

        【讨论】:

          猜你喜欢
          • 2013-09-23
          • 2023-03-18
          • 1970-01-01
          • 2014-02-27
          • 2013-02-25
          • 1970-01-01
          • 1970-01-01
          • 2013-05-23
          • 1970-01-01
          相关资源
          最近更新 更多