安卓,XML : Show only portion of image in layout container without altering aspect ratio

标签 android xml imageview

我想为任何布局设置背景。 通常,我会继续这样下去:

<LinearLayout
    android:orientation="vertical"
    android:background="#@drawale/somedrawable"
    android:gravity="center"
    android:layout_width="match_parent"
    android:layout_height="100dp"/>

但这将始终使背景适合 LinearLayout。但是,如果布局的高度小于我设置为背景的图像怎么办?我不想想要破坏图像的宽高比,只是将图像置于布局的中心并让高度重叠,这样它就不再可见了。

澄清一下:

左边是现在发生的事情,右边是我想成为的样子。由于布局容器小于 imageview 或背景图像,它应该保持居中但只显示适合的内容而不改变纵横比。

最佳答案

在宽度和高度 match_parent 的线性布局中使用 ImageView 并设置 scaleType="centerCrop"

<ImageView
    android:src="@drawable/somedrawable"
    android:scaleType="centerCrop"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

关于安卓,XML : Show only portion of image in layout container without altering aspect ratio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53446503/

相关文章:

android - Android 中的默认动画在哪里?

xml - 对 XML 进行递归排序 - 仅对内部节点进行排序

html - 如何通过 XPath 选择第一个元素?

android - 使用 picasso 将相同的图像设置为 ImageView 的背景

android - API 21+ : AppCompat v23. 2.0 上的模糊图像使用带有 srcCompat 的 VectorDrawables

java - 使用变量在 Android Studio 中使用 setImageResource 设置图像

android - 在 BluetoothGattCallback 中使用 Content Provider 时的安全问题

Android Studio 图像分辨率

android - 将xml插入到SQLite数据库中并在Fragment中显示数据

xml - Play Framework 记录器在打包后不选择格式