When I first encountered layout layouts in Android, with all of these FrameLayout, LinearLayout, RelativeLayout. With the concepts of weight and gravity for interface elements. I wondered why it was impossible to do how it has long been done in html. Where can I specify markup in percent? And now, finally, this opportunity appeared. It appeared of course not yesterday, but I stumbled upon it just now, and I did not find articles on the topic with which I eat it.

So, what is needed in order to feel this happiness? You need quite a bit, open the build.gradle of your application, add a line there
')
dependencies { compile 'com.android.support:percent:23.4.0' }
and synchronize.
Everything, all the charms of markup as a percentage are available to you, at least for minSDK 14 (I have not checked it before).
The blank layout will look like this:
<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" android:theme="@android:style/Theme.Black"> </android.support.percent.PercentRelativeLayout>
We can use the following attributes to specify the size and position of the child elements.
- layout_widthPercent
- layout_heightPercent
- layout_marginPercent
- layout_marginLeftPercent
- layout_marginTopPercent
- layout_marginRightPercent
- layout_marginBottomPercent
- layout_marginStartPercent
- layout_marginEndPercent
- layout_aspectRatio
In this case, you need to specify the prefix not of
android: layout_widthPercent , but
app: layout_widthPercent , in accordance with the namespace specified in the layout header. Values are assigned to these attributes in percent, with the obligatory indication of the% sign.
Actually, the assignment of almost all attributes is intuitive, the width, height, and padding in percent of the parent layout.
It’s probably worth defining only layout_aspectRatio. This attribute gives you the ability to set the aspect ratio of an element. For example, you want to create a square button that takes 15% of the screen width. You specify layout_widthPercent = "15%", if you specify layout_heightPercent = "15%" then you get a rectangular button. Therefore, you need not specify the layout_heightPercent, but specify the layout_aspectRatio = "100%". In this case, the width will be calculated by the formula:
layout_heightPercent * layout_aspectRatio / 100.Another question may arise: how does layout_marginStartPercent differ from layout_marginLeftPercent, and layout_marginEndPercent from layout_marginRightPercent, respectively? Everything is simple, it is to ensure the localization of the interface, for those languages that are read from left to right, Start = Left, and for those that are right to left, Start = Right.
PercentRelativeLayout is a successor of RelativeLayout, so along with Percent attributes, you can use RelativeLayout attributes, for example, you can specify the height of the button as android: layout_height = "wrap_content" and the width of the app: layout_heightPercent = "25%".
Of course PercentRelativeLayout is not a panacea, for example, turning the screen may result in interesting and not pleasant special effects. Experiment yourself, and for myself I concluded that when using PercentRelativeLayout, you must make a Landscape layout version.