android - 如何停止在linearlayout上拉伸背景图像

时间:2015-09-09 11:20:50

标签: android

我在线性布局上将图像设置为背景。这是代码:

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

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

我该怎么办?

谢谢

2 个答案:

答案 0 :(得分:4)

在布局中添加图片视图,并将其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

答案 1 :(得分:4)

您可以使用RelativeLayout并将带有scaleType的ImageView放入其中,并使用包含所有内容的LinearLayout。

<RelativeLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

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

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

    -----Your Content----

  </LinearLayout>
</RelativeLayout>