在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保留在背景上。希望这可以帮助!
答
代码示例叶戈尔的回答是:
<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>
这对我工作,非常感谢! – Joris 2011-05-04 08:28:14
这很好,但我认为更优雅的解决方案,可以在这里找到:http://stackoverflow.com/questions/2781593/background-image-placement – iganev 2013-10-08 11:52:32