I'm having the following problem;
I have a LinearLayout with in it a ScrollView, in the ScrollView is some custom view. Now I want to put an image in the background of the LinearLayout and keep the image in its original size. But when I set the background property of the LinearLayout it stretches to fit the full screen. How can I make it so that the image keeps its original size?
My 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>
Code example for Egor's answer:
Try using FrameLayout with an ImageView and LinearLayout inside. For example, try changing the alpha of the image and move it to foreground in your FrameLayout, thus the LinearLayout stays on background. Hope this helps!
Alternatively to ColdForged's suggestion, you could move from
LinearLayout
toRelativeLayout
and useImageView
to show the image instead of changing background to it. The views inRelativeLayout
can interfere unlike inLinearLayout
.You might take a look at the InsetDrawable. Either that or it would likely require you to subclass your layout to draw the way you wish as the background element doesn't have the ability you need.