It so happened that all the elements in my activity should be arranged in%. The easiest way I have not passed:


What is the way to position elements using absolute (%) layout?

    3 answers 3

    Not so long ago, Google introduced a new support library : percent , which allows you to set some parameters of widgets as a percentage of the total container size.

    The library includes 2 containers that support the indication of the elements nested in them as a percentage: PercentRelativeLayout and PercentFrameLayout . The properties of the containers are similar to the "older" brothers, as they allow you to specify some parameters in percent.

    Example 1:

     < xmlns:android="" xmlns:app="" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_height="wrap_content" app:layout_widthPercent="35%" app:layout_heightPercent="10%" app:layout_marginLeftPercent="10%" app:layout_marginStartPercent="10%" android:text="Some text" /> <TextView android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:padding="8dp" android:text="Another some text" app:layout_marginEndPercent="10%" app:layout_marginRightPercent="10%" app:layout_widthPercent="35%" /> </> 

    Example 2:

     < xmlns:android="" xmlns:app="" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:background="#f0f0f0" app:layout_widthPercent="50%" app:layout_heightPercent="50%" app:layout_marginTopPercent="25%" app:layout_marginLeftPercent="25%" /> </> 

    In order to connect the library, add the dependency to the dependencies section of the application's build.gradle file:

    compile ''

    23.0.0 - the current latest version of the library may change over time.

    More details on usage

      Wrapped in LinearLayout, for the children we set the desired layout_weight.

       <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <!-- Эта кнопка занимает 30% от всей ширины --> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.3" /> <!-- Эта - 50% --> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.5" /> <!-- А эта - 20% --> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="0.2" /> </LinearLayout> 
      • weight is the width. And how to change the height. And the distance from the side (top, left)? - kandi
      • Horizontal indents - in the same way, insert the View-strut, set the desired layout_weight to it. <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: orientation = "horizontal"> <! - Indent to the left by 20% -> <View android: layout_width = "0dp" android: layout_height = " 0dp "android: layout_weight =" 0.2 "/> <Button android: layout_width =" 0dp "android: layout_height =" wrap_content "android: layout_weight =" 0.8 "/> </ LinearLayout> With vertical indents and height so, alas, do It will not work, this is only software. - falstaf
      • @falstaf and how program? Either write the code, or give links to manuals on this topic, pliz. - kandi

      Quote: weight is the width.

      weight - the "weight" of the object or the same percentages depending on the given orientation: in this case, since layout orientation is horizontal, then we can say that it is width.

      You can create a style in which the necessary attributes will be specified. Later this style can be applied to any button.