Percentage layout

Keywords: Android Junit Attribute Gradle

In the commonly used layout LinearLayout, relativelayout and FrameLayout, only LinearLayout supports the function of using the layout "weight attribute to specify the size of the control in proportion, and neither of the other two layouts supports it.
For this reason, Android has introduced a new layout method to solve this problem -- percentage layout. In this layout, we can no longer use wrap content, match parent and other methods to specify the size of the control, but allow to directly specify the percentage of the control in the layout.
Because LinearLayout itself supports specifying the size of the control in proportion, the percentage layout only extends the function of RelativeLayout and FrameLayout, that is, it provides two new layouts, PercentRelativeLayout and PercentFrameLayout.

How to use

  • Open the app/build.gradle file, and add the following contents to the dependencies closure:
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:percent:24.2.1'
}
  • Example:
<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">

    <Button
        android:id="@+id/button1"
        android:text="Button 1"
        android:layout_gravity="left|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/button2"
        android:text="Button 2"
        android:layout_gravity="right|top"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/button3"
        android:text="Button 3"
        android:layout_gravity="left|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

    <Button
        android:id="@+id/button4"
        android:text="Button 4"
        android:layout_gravity="right|bottom"
        app:layout_widthPercent="50%"
        app:layout_heightPercent="50%"
        />

</android.support.percent.PercentFrameLayout>

Explain:
PercentFrameLayout and PercentRelativeLayout inherit all the properties in FrameLayout and RelativeLayout respectively, and you can use app: layout ﹐ widthpercent and app: layout ﹐ hightpercent to specify the width and height of the control by percentage.

Posted by satheshf12000 on Thu, 02 Apr 2020 15:18:00 -0700