19

我有这个简单的布局 XML 和一个 30X30 像素的 green_square.png 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:background="@color/red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitStart"
        android:src="@drawable/green_square"/>

</LinearLayout>

我想要实现的是缩放图像以填充父宽度,保持它的纵横比而不需要 ImageView 的额外空间。

我为 ImageView 添加了一个红色背景,只是为了参考我想要避免的额外空间。

这是我的目标

4

6 回答 6

45
<ImageView
        android:id="@+id/game"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_game" 
        />

这对我有用。父裁剪和中心裁剪的全宽以保持纵横比

于 2013-03-24T15:25:17.530 回答
1

It's so simple. Just make the width fill_parent and height wrap_content. Here is the I figured it out.

<ImageView
    android:id="@+id/game"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/ic_game" 
    />
于 2016-10-06T05:31:46.197 回答
0

在我看来,这个ImageView额外的布局是一个错误。我也发生了同样的事情。结果,我想要遵循的文本遵循green_square额外的布局。所以我所做的是指定相对于屏幕底部的文本。这样,文本将超过额外的布局。我承认,这不是最好的解决方案,但它看起来比green_square在文本和文本之间有一个巨大的空间要好得多。

于 2012-05-02T21:02:25.657 回答
0

这里有很多很好的答案,它们确实有效。我只是分享一下我现在使用更现代的 XML 语法(约束视图)执行此操作的方式。相信我约束视图车!

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#394BAB"
    tools:context="com.appdomain.app.apppro.ActivityName">

    <ImageView
        android:id="@+id/nice_img"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:src="@drawable/green_square"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

当然 tools:context="com.collectsavings.collect.collectpro.LoginAndRegisterPrompt" 和 android:src="@drawable/green_square" 应该都分别匹配您的应用活动上下文和可绘制资源。

于 2019-11-19T20:21:00.873 回答
-1

使用比例类型“fitCenter”或“fitxy”

于 2012-01-03T10:12:58.753 回答
-4

尝试这个,

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/red"
    android:scaleType="centerCrop"
    android:src="@drawable/green_square" />

</LinearLayout>
于 2012-01-03T10:24:29.003 回答