我想为任何布局
设置背景。
通常,我会继续这样下去:
<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/