Search code examples
androidandroid-layoutandroid-percentrelativelayoutandroid-percent-librarypercent-support-library

how to set ImageView width using Percent support lib and maintain aspect ratio?


Using the Android Percent Support Library, how can I set the width of an ImageView to a percentage of its containing PercentRelativeLayout, while scaling the height in such a way as to maintain the original aspect ratio?


Solution

  • I got this to work with version 23.0.0 support lib:

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:src="@drawable/logo"
            app:layout_widthPercent="50%"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true" />
    
    </android.support.percent.PercentRelativeLayout>