android - 如何停止在线性布局上拉伸(stretch)背景图像

标签 android

我已在 linearlayout 上将图像设置为背景。这是代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/mainbg"
    android:orientation="vertical"
    android:padding="5dp"
    >

问题是,图像的宽度约为 2000 像素,我不想缩小尺寸以适应屏幕,我只想显示尽可能多的背景而不是拉伸(stretch)它。

我该怎么做?

谢谢

最佳答案

在布局中添加 ImageView 并将其 scaletype 设置为 centerCrop

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="5dp">

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

</LinearLayout>

检查图像比例类型:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

关于android - 如何停止在线性布局上拉伸(stretch)背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32478186/

相关文章:

android - 当我调用 Activity 启动器方法时,应用程序随机崩溃

android - 在 android 中运行模拟器期间出现内部错误

android - 将字符串值从自定义对话框传递到另一个 Activity

android - SoundPool 不能播放 mp3?

java - 在 Android Studio 上调试基于 gradle 的 Java 项目?

android - 检测手机上的向前/向后运动

android - 标签栏从 android studio 中消失了

java - 从 Android 项目的 Drawable 中获取图像?

php - $_SERVER ['HTTP_REFERER' ] 在移动网络应用程序中的等效功能是什么

php - 不将我在 android 中的编辑文本值传递给 PHP 变量