Android Layouts suggest change

Percent Layouts

The Percent Support Library provides PercentFrameLayout and PercentRelativeLayout, two ViewGroups that provide an easy way to specify View dimensions and margins in terms of a percentage of the overall size.

You can use the Percent Support Library by adding the following to your dependencies.

compile 'com.android.support:percent:25.3.1'

If you wanted to display a view that fills the screen horizontally but only half the screen vertically you would do thie following.

<android.support.percent.PercentFrameLayout
    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">

    <FrameLayout
        app:layout_widthPercent="100%"
        app:layout_heightPercent="50%"
        android:background="@android:color/black" />

<android.support.percent.PercentFrameLayout>

You can also define the percentages in a separate XML file with code such as:

<fraction name="margin_start_percent">25%</fraction>

And refer to them in your layouts with @fraction/margin_start_percent.

They also contain the ability to set a custom aspect ratio via app:layout_aspectRatio.

This allows you to set only a single dimension, such as only the width, and the height will be automatically determined based on the aspect ratio you’ve defined, whether it is 4:3 or 16:9 or even a square 1:1 aspect ratio.

For example:

<ImageView
   app:layout_widthPercent="100%"
   app:layout_aspectRatio="178%"
   android:scaleType="centerCrop"
   android:src="@drawable/header_background"/>

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents