简体   繁体   中英

Android how to overlap an ImageView on top of a LienearLayouy so that they have the same size?

Hi and thanks for any suggestion.

I need to put an ImageView to overlapp over a LinearLayout.

I have tried this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

 >

<ImageView
android:id="@+id/backgroundimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/gradient_box"
android:layout_gravity="center"
 />


<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_gravity="center">

But this is the result I get (the ImageView shrinks to a single line) 在此处输入图片说明

I have tried using RelativeLayout, this is the result I get.

CLOSE, but the ImageView now expands to fill all the available space and does not "wrap content" as I need to.

在此处输入图片说明

The desired result would be like this: 在此处输入图片说明

PS: I cannot use android:background="image" because i need to change it at runtime.

You can use a Relativ Layout that contains the LinearLayout and then align the ImageView with the LinearLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/background"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

   <LinearLayout
    android:id="@+id/linear"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:layout_gravity="center">

   <ImageView
     android:id="@+id/backgroundimage"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/gradient_box"
     android:layout_alignLeft="@id/linear"
     android:layout_alignRight="@id/linear"
     android:layout_alignTop="@id/linear"
     android:layout_alignBottom="@id/linear"
     android:layout_gravity="center"
     android:scaleType="fitCenter" />

If you just want to set the image as a background you can just set a background image for the linear layout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/background"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/blue" >

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_centerHorizontal="true"
   android:layout_centerVertical="true"
   android:orientation="vertical"
   android:background="@drawable/gradient_box"
   android:layout_gravity="center">        

PS: I cannot use android:background="image" because i need to change it at runtime.

Why not? Use setBackgroundResource .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM