Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout: how to make image to change its width and height proportionally?

I have such layout:

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

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/my_image"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Some text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/title"
    android:layout_alignBottom="@+id/title"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:src="@drawable/my_bitmap_image" />

This layout does almost what I need: it makes image view height the same as text view. The image graphic contents stretched also keeping aspect ratio.

But, the width of the image view does not change! As a result, I have a wide gap between text and the image view! As a temporal solution, I override View#onLayout.

The question: how to change image width in xml layout?

UPDATE:

This is a final layout I need (text + a few images). Look at the first image: its width should be exactly the same as scaled image in it with no paddings and margins:

enter image description here

like image 406
Exterminator13 Avatar asked Oct 19 '25 20:10

Exterminator13


1 Answers

For the imageView you can add the images to a linearlayout and give the weight property. For example if you have 3 images then give the linearlayout weight as 3 and then for each image you give the weight as 1. This way it will be uniformly aligned with equal width for all the images. Make linear orientation as horizontal hope so u got my point.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="3"
     >
   <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />

    <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />

    <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />
     </LinearLayout>
like image 67
bostan Avatar answered Oct 22 '25 11:10

bostan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!