4

我有一个这样的RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                       android:id="@+id/favoriteLinearLayout"
                       android:layout_width="fill_parent"
                       android:layout_height="wrap_content"
                       android:orientation="vertical">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background1"
        >
    ....
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background2"
            android:layout_below="@+id/linearLayout1"
        >
    ....
    </LinearLayout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/background3"
            android:layout_below="@+id/linearLayout2"
        >
    ....
    </LinearLayout>

</RelativeLayout> 

我想在三个 LinearLayout 中的每一个中都有一个不同的背景图像,并且应该缩放这个图像以填充它的 LinearLayout,而不保留图像的纵横比。而且我不希望每个LinearLayout都被缩放以适应图像的大小(如果图像高于LinearLayout,我希望降低图像高度)。

此后它应该是什么样子:

在此处输入图像描述

任何人都知道如何做到这一点?

谢谢 !!

4

1 回答 1

4

您可以使用 imageView 并将其 scaleType 设置为 fitXY。这是一个例子:

<FrameLayout  android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView 
      android:layout_width="fill_parent" android:layout_height="fill_parent"
      android:src="@drawable/blue_dark_background" android:scaleType="fitXY" />

    <LinearLayout>....</LinearLayout>
</FrameLayout>

如果这对您有帮助,请告诉我。问候!

编辑

这是一个更清楚的例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:weightSum="3"
    android:orientation="vertical">

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>
<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="1" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY"
        android:src="@drawable/ic_menu_share" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </LinearLayout>
</FrameLayout>

</LinearLayout>
于 2013-02-01T18:19:07.297 回答