Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

percentage margin with constraintlayout?

I'm learning how to use ConstraintLayout and have found a few good tutorials to have views width and height in percentage. I know I can probably add an empty view to 'create' margin, but it doesn't seem right. Is there a way to things like marginEnd='10%'?

like image 267
user1865027 Avatar asked Dec 18 '17 18:12

user1865027


People also ask

How do you use percent constraint layout?

You can currently do this in a couple of ways. One is to create guidelines (right-click the design area, then click add vertical/horizontal guideline). You can then click the guideline's "header" to change the positioning to be percentage based. Finally, you can constrain views to guidelines.

What are ConstraintLayout constraints?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).

Can we use LinearLayout in ConstraintLayout?

Most of what can be achieved in LinearLayout and RelativeLayout can be done in ConstraintLayout.

Why do we prefer constraint ConstraintLayout in Android?

Constraint Layout simplifies creating complex layouts in Android by making it possible to build most of your UI using only the visual editor in Android Studio. Constraint Layout comes with some powerful tools with the help of which you can define complex layouts without having deep nesting.


1 Answers

There're two ways to add percentage margin using ConstraintLayout.

#1 Guideline

Simply add vertical guideline to your layout and constraint view to that guideline:

<android.support.constraint.ConstraintLayout     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">      <View         android:id="@+id/view"         android:layout_width="0dp"         android:layout_height="50dp"         android:background="@color/colorAccent"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toStartOf="@+id/guideline"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />      <android.support.constraint.Guideline         android:id="@+id/guideline"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:orientation="vertical"         app:layout_constraintGuide_percent="0.9" />  </android.support.constraint.ConstraintLayout> 

#2 Constraint weight

Add Space, place it on the very left side of parent, group your view and Space into "spread" chain and set app:layout_constraintHorizontal_weight="90" to your view and app:layout_constraintHorizontal_weight="10" to Space. It works very similar to LinearLayout's weight.

<android.support.constraint.ConstraintLayout     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">      <View         android:id="@+id/view"         android:layout_width="0dp"         android:layout_height="50dp"         android:background="@color/colorAccent"         app:layout_constraintHorizontal_chainStyle="spread"         app:layout_constraintHorizontal_weight="90"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintEnd_toStartOf="@+id/space"         app:layout_constraintBottom_toBottomOf="parent" />      <Space         android:id="@+id/space"         android:layout_width="0dp"         android:layout_height="wrap_content"         app:layout_constraintHorizontal_weight="10"         app:layout_constraintTop_toTopOf="parent"         app:layout_constraintStart_toEndOf="@+id/view"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintBottom_toBottomOf="parent" />  </android.support.constraint.ConstraintLayout> 

As a result, your view has end margin with value of 10% of parent width:

Result view

like image 120
Eugene Brusov Avatar answered Oct 05 '22 16:10

Eugene Brusov