pkraau pkraau - 5 months ago 16
Android Question

Android how to resize a layout after its image was resized

I have a layout with 3 images side-by-side. I use layout_weight to set them up :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#888888"
tools:context="test_layout_resize.MainActivity">

<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

<RelativeLayout
android:id="@+id/layout1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hotdog"/>
</RelativeLayout>

<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

<RelativeLayout
android:id="@+id/layout2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hotdog"/>
</RelativeLayout>

<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

<RelativeLayout
android:id="@+id/layout3"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/image3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/hotdog"/>
</RelativeLayout>

<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"/>

</LinearLayout>


For some reason, layout1, 2 and 3's height is that of the original hotdog image, not the shrunken one :

3 hotdogs not at the top of the screen

I tried getParent().requestLayout() or invalidate() in ImageView.onDraw(), neither of which changed anything.

How do I make the RelativeLayout's height resize to the new ImageView height ?

Neo Neo
Answer

Use android:adjustViewBounds="true" inside your all ImageView will solve your problem.

Hope it will help you :)

Comments