ImageView的是一个固定的大小,而不管图像尺寸的的是、图像、尺寸、大小

2023-09-04 11:15:09 作者:单纯的喜欢而已

我有,我想永远是相同尺寸的ImageView的。如果图像比视图小,我想它在视图中居中。如果图像比查看大话,我想它缩小 - 宽高比为preserved

I have an ImageView which I want to always be the same size. If the image is smaller than the view, I'd like it to be centred in the view. If the Image is larger than the view then I'd like it scaled down - with aspect ratio preserved.

我在这过几次尝试自己 - 最近的是:

I've had several attempts at this myself - the latest of which is:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView android:id="@+id/itemImage" android:layout_width="100dp"
            android:layout_height="100dp" android:scaleType="fitCenter"
            android:adjustViewBounds="true" android:gravity="center" android:padding="10px" />
  <TextView android:id="@+id/currentLentItem" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:padding="10px"/>
</LinearLayout>

我是什么在这里失踪?

What am I missing here?

推荐答案

尝试使用规模型 centerInside ,如下图所示:

Try using the scale type centerInside, like below:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
    <ImageView
        android:id="@+id/itemImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentRight="true"
        android:scaleType="centerInside"
        android:gravity="right|center_vertical"
        android:padding="10px"
        />
    <TextView
        android:id="@+id/currentLentItem"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@id/list_image"
        android:padding="10px"
        />
</RelativeLayout>

这使得图像缩减仅比大的布局,它会按比例缩放,以及(而fitXY鳞不考虑宽高比)。它还将如果较小图像居中

This causes the image to scale down only if larger than the layout, and it will scale proportionally as well (whereas fitXY scales without regard to aspect ratio). It will also center the image if smaller.

编辑:我已经更新的例子的东西,就像你想。这里有什么改变的要点:

I've updated the example for something like you're wanting. Here's the gist of what changed:

•ImageView的比重改为右| center_vertical 。既然你想让他们对准右侧,使用这种重力将允许即使是纵向的图像。

• ImageView gravity changed to right|center_vertical. Since you want them to align on the right, using this gravity will allow that even if they are portrait orientation images.

•ImageView的对齐RelativeLayout的权

• ImageView aligned to the right of the RelativeLayout

•RelativeLayout的设置为 WRAP_CONTENT ,因为它是用于一个ListView。

• RelativeLayout set to wrap_content since it's to be used for a ListView.

•TextView的加入,对准到父的左,与由itemImage的左边缘限定它的右边缘。

• TextView added, aligned to the parent's left, with its right edge defined by the left edge of itemImage.