在LinearLayout中缩放背景图像

在LinearLayout中缩放背景图像

问题描述:

我遇到以下问题;在LinearLayout中缩放背景图像

我有一个LinearLayout在其中一个ScrollView,在ScrollView中是一些自定义视图。现在我想在LinearLayout的背景中放置一个图像并保持图像的原始大小。但是当我设置LinearLayout的背景属性时,它会拉伸以适应全屏。我怎样才能让图像保持原有尺寸?

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/some_drawable"> 
    <ScrollView 
    android:id="@+id/scrollview" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:fillViewport="true"> 
    <some.custom.layout 
     android:id="@+id/mainLayout" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     /> 
    </ScrollView> 
</LinearLayout> 

尝试在内部使用带有ImageView和LinearLayout的FrameLayout。例如,尝试更改图像的alpha值并将其移动到FrameLayout中的前景中,因此LinearLayout保留在背景上。希望这可以帮助!

+0

这对我工作,非常感谢! – Joris 2011-05-04 08:28:14

+1

这很好,但我认为更优雅的解决方案,可以在这里找到:http://stackoverflow.com/questions/2781593/background-image-placement – iganev 2013-10-08 11:52:32

你可能需要看看InsetDrawable。无论是这样或者它可能需要你的布局的子类来绘制你想要的方式,因为背景元素没有你需要的能力。

除了ColdForged的建议,您可以从LinearLayout移动到RelativeLayout并使用ImageView来显示图像,而不是更改背景。 RelativeLayout中的意见可能会与LinearLayout不同。

+0

我已经试过这一点,但我怎么画了滚动的内容在图像视图,使得图像的背景? – Joris 2011-04-29 15:34:29

+1

也许通过设置为滚动视图背景alpha的 – ernazm 2011-04-29 15:37:05

代码示例叶戈尔的回答是:

<FrameLayout 
    android:id="@+id/FrameLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="5dp" 
    > 

    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:src="@drawable/background" 
     /> 

    <LinearLayout 
     android:id="@+id/LinearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 

    </LinearLayout> 

</FrameLayout>