我有这个简单的布局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中添加了一个红色背景,仅供参考我想要避免的额外空间。
答案 0 :(得分:37)
<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"
/>
这对我有用。父和中心裁剪的全宽以保持纵横比
答案 1 :(得分:0)
在我看来,这个ImageView
额外布局是一个错误。我有同样的事情发生。因此,我想跟随green_square
的文字遵循额外的布局。所以我所做的是指定相对于屏幕底部的文本。这样,文本将覆盖额外的布局。我承认,这不是最好的解决方案,但它比green_square
和文本之间有一个巨大的空间要好得多。
答案 2 :(得分:0)
这很简单。只需设置宽度fill_parent和height wrap_content即可。这是我想出来的。
<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"
/>
答案 3 :(得分: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>
当然 工具:context =“ com.collectsavings.collect.collectpro.LoginAndRegisterPrompt” 和 android:src =“ @ drawable / green_square” 所有这些都应分别与您的应用活动上下文和可拖动资源匹配。
答案 4 :(得分:-1)
使用比例类型“fitCenter”或“fitxy”
答案 5 :(得分:-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>