Scale background image to wrap content of layout

北战南征 提交于 2019-11-29 21:22:58

I had the same problem (I think), and the only solution I could find was this:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/some_image"
        android:layout_alignTop="@+id/actual_content"
        android:layout_alignBottom="@id/actual_content"
        android:layout_alignLeft="@id/actual_content"
        android:layout_alignRight="@id/actual_content"
        />

    <LinearLayout
        android:id="@id/actual_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        >

       <!-- more stuff ... -->

    </LinearLayout>

</RelativeLayout>

You may try this trick,

  • FrameLayout (wrapcontent, wrapcontent)
    • ImageView (match parent, match parent) // its your background
    • LinearLayout (wrapcontent, wrapcontent)
      • All your contents goes inside this linear layout or any other type what ever you want

Work with RealtiveLayout (no mandatory but instead of FrameLayout and use android:scaleType="fitXY". Move the imageView to RealtiveLayout which contains the textviews android:id="@+id/HeaderList" and unset the background android:background="@drawable/header" at this level.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
         android:id="@+id/principal"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:background="@drawable/header" >

     <RelativeLayout 
         android:layout_width="fill_parent"
         android:id="@+id/HeaderList" 
         android:layout_gravity="top"
         android:layout_height="wrap_content" >  


         <ImageView
             android:id="@+id/backImg"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:layout_centerInParent="true"
             android:adjustViewBounds="true"
             android:background="@color/blancotransless"
             android:src="@drawable/header"
             android:scaleType="fitXY" >
         </ImageView>

             <TextView 
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content" 
                 android:id="@+id/NameText"
                 android:text="John Doe" 
                 android:textColor="#FFFFFF"
                 android:textSize="30sp" 
                 android:layout_alignParentLeft="true"
                 android:layout_alignParentTop="true" 
                 android:paddingLeft="4dp"
                 android:paddingTop="4dp" 
                 />
             <TextView 
                 android:layout_height="wrap_content"
                 android:layout_width="wrap_content" 
                 android:textColor="#FFFFFF"
                 android:layout_alignParentLeft="true" 
                 android:id="@+id/HoursText"
                 android:text="170 hours" 
                 android:textSize="23sp"
                 android:layout_below="@+id/NameText" 
                 android:paddingLeft="4dp" 
                 />
         </RelativeLayout> 
      </RelativeLayout>

More or less it should work!

Create a drawable xml, say mybackground.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/header"
    android:gravity="fill" />

Then in your RelativeLayout in the first code snippet use

android:background="@drawable/mybackground"

I think I've done something similar to this in the past, try playing with the gravity setting in your image view, I think one of them should do what you're after.

I have not tried it, but if you set the background image dynamically on the onCreate that might work. The size of the layout should already be set.

You can use this:

<ImageView
...
android:scaleType="fitXY"
>

Hope it helped)))

Try this code.....

<LinearLayout 
   android:layout_width="fill_parent"
   android:id="@+id/HeaderList" 
   android:orientation="vertical"
   android:layout_gravity="top"
   android:layout_height="wrap_content" 
   android:background="@drawable/header">
<TextView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:id="@+id/NameText"
    android:text="Jhn Doe" 
    android:textColor="#FFFFFF"
    android:textSize="30sp" 
    android:paddingLeft="4dp"
    android:paddingTop="4dp" 
/>
<TextView 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:textColor="#FFFFFF"
    android:id="@+id/HoursText"
    android:text="170 hours" 
    android:textSize="23sp" 
    android:paddingLeft="4dp" 
/>
</LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!