Margin/padding In Percentage In Xml
Solution 1:
it is not possible, though You better do it by taking width and height of screen like below in your java code, and
Displaydisplay= getWindowManager().getDefaultDisplay();
intwidth= display.getWidth();
intheight= display.getHeight();
then calculate your margin from screen size, and set it by
setmargin(int x) //method of view/layout.
this way you can set margin depending on the screen size not fixed for all screen, and basically it is like setting in percentage but from Java code.
Solution 2:
It became possible with Guidelines introduced in ConstraintLayout.
Here's the example of TextView placed at 60% of screen width and 25% of screen height:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayoutxmlns: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"><TextViewandroid:id="@+id/textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="TextView"app:layout_constraintLeft_toLeftOf="@+id/guideline1"app:layout_constraintTop_toTopOf="@+id/guideline2" /><android.support.constraint.Guidelineandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/guideline1"android:orientation="vertical"app:layout_constraintGuide_percent="0.6" /><android.support.constraint.Guidelineandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/guideline2"android:orientation="horizontal"app:layout_constraintGuide_percent="0.25" /></android.support.constraint.ConstraintLayout>
Solution 3:
If you are trying to share sreen width between your controls in percentage. Best way to do that in android is using weight
property for them. Check following URL for more details.
regards, Aqif Hamid
Solution 4:
Though it is quite late, it can be done with Android percentageRelativeLayout https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html One can use percentage with some attributes but not all are supported. Like padding is still not supported in percentage whereas margin is.
Post a Comment for "Margin/padding In Percentage In Xml"