【问题标题】:Android: how to scale an image to fill the background of a LinearLayout without preserving aspect ratio?Android:如何在不保留纵横比的情况下缩放图像以填充 LinearLayout 的背景?
【发布时间】:2013-01-17 01:49:54
【问题描述】:

我有一个这样的 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,我希望降低图像高度)。

以后应该是什么样子:

有人知道怎么做吗?

谢谢!!

【问题讨论】:

    标签: android xml image android-linearlayout android-framelayout


    【解决方案1】:

    您可以使用 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>
    

    【讨论】:

    • 感谢您的回答,但它不适用于我的具体情况。更准确地说,我有一个 RelativeLayout,里面有 3 个连续的 LinearLayout。我想为每个 LinearLayout 使用不同的背景图像(适合 LinearLayout 大小而不保留纵横比)。我将更新我的问题以更准确。
    • 我刚刚修改了我的问题。希望你能帮助我!
    • 在这种情况下,它将是 3 个不同高度的图像视图(最好以重量为单位)。
    • 如果您能提供几行代码,我将不胜感激。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-11-20
    • 2014-12-11
    • 1970-01-01
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    相关资源
    最近更新 更多