Using PercentRelativeLayout specifying only width (heigh auto)

123 Views Asked by At

It is possible to use PercentRelativeLayout specifying only the desired width dimension and getting automatic heigh?

I mean doing this:

<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
    android:id="@+id/earthImageView"
    app:layout_widthPercent="30%"
    android:src="@drawable/earth"
    android:layout_alignParentRight="true"
    android:adjustViewBounds="true"/>
</android.support.percent.PercentRelativeLayout >

The view has the correct width but if I check the layout boundaries I can see that the height is MATCH_PARENT. It means hat android:adjustViewBounds="true" is not working.

I tried adding app:layout_widthPercent="WRAP_CONTENT" but has no effect.

Edit

Also having problems when doing the inverse situation:

<ImageView
        android:id="@+id/earthImageView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_heightPercent="20%"
        android:src="@drawable/earth"
        android:adjustViewBounds="true"/>
1

There are 1 best solutions below

4
On BEST ANSWER

If I understand correctly you want to specify the width and have the height set automatically to whatever adjustViewBounds gives you.

If that's the case, the following should do what you want (I've set both of the layout and imageView heights to wrap_content):

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/earthImageView"
        android:layout_height="wrap_content"
        app:layout_widthPercent="30%"
        android:src="@drawable/lapin"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"/>
</android.support.percent.PercentRelativeLayout >