ImageView fit to the content layout

时间:2015-07-28 16:34:29

标签: android background imageview

In my layout I would like to use an ImageView as background (with scaleType:centerCrop).

This requires my ImageView fits in height to the content of my block.

Here is my layout.xml , but the ImageView doesn't fit to the content.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/img_couverture"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:scaleType="centerCrop" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <!-- Other widgets --> 

</LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

Try this - seems to work for me -(NB switched to using LinearLayout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">

   <ImageView
       android:id="@+id/img_couverture"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:adjustViewBounds="true"
       android:scaleType="centerCrop"
       android:src="@drawable/ic_launcher" />

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
   </LinearLayout>

</LinearLayout>