9

I have a linear layout like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:background="@drawable/bgapp" >

What I need now is the possibility to set the background image ScaleType value in order to fill the screen everytime, this because I'm going to download the image and then I'm going to set it as background but without ScaleType the images will not keep it's aspect ratio based on the screen densities.

What I found is this solution:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/bgapp"
    android:scaleType="centerCrop"
    android:gravity="bottom|left" />

But It doesn't work as expected.. Is there a way to set a LinearLayout scaleType property or do I need to change my layout?

LS_
  • 6,763
  • 9
  • 52
  • 88

1 Answers1

18

View's background is stretched depending on the size of the View.

Image scaling is supported by ImageView, and to use it together with the LinearLayout you need an additional container, that will overlay the 2 children (e.g. a FrameLayout):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/bgapp"
    android:scaleType="centerCrop"/>

  <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    ...
  </LinearLayout>
</FrameLayout>
Simas
  • 43,548
  • 10
  • 88
  • 116
  • @pskink yes you may be right about the custom drawable. I was talking about how `View` background attribute works in general. – Simas Oct 12 '15 at 14:42
  • yeah I saw this type of solution too but I didn't want to change my xml structure just for a background image, I'm still hoping for a solution that allows LinearLayout to have the scaleType options. thanks anyways! – LS_ Oct 12 '15 at 15:35
  • did you get any sollution? @Signo – Prashanth Debbadwar Dec 03 '15 at 09:29
  • @PrashanthDebbadwar lol I was just searching for one right now :D still nothing i'm sorry :( btw this is a correct solution, I just didn't want to change my xml – LS_ Dec 03 '15 at 09:30
  • Not worked. I need use `ScalingFrameLayout` for my aim. – Huy Tower Dec 26 '16 at 03:53
  • Using `ImageView` you'll satisfy accessibility. Imaging they have `TextView`s inside this `LinearLayout`. `ImageView` won't stretch to accommodate space for increased font – pushandpop Aug 30 '22 at 16:44