Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify margins for Barrier in Constraintlayout

I am unsure about the best way to specify margins in ConstraintLayout around a Barrier.

I tried setting them in the barrier element, but this has no effect and I also couldn't find any documentation on that.

   <androidx.constraintlayout.widget.Barrier
            android:id="@+id/detail_barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="top"
            android:layout_marginBottom="8dp"
            app:constraint_referenced_ids="detail_header_1,detail_header_2" />
like image 645
Display name Avatar asked Oct 28 '19 11:10

Display name


People also ask

How is barrier used in ConstraintLayout?

Creating Barriers in XMLThe app:barrierDirection attribute determines the direction of the Barrier - in this case it will be positioned at the end of the referenced Views. The list of referenced Views is a comma separated list of the IDs of other Views within the layout (without the @+id/ qualifier).

How do I use guideline in ConstraintLayout?

The following steps can be used to make use of guidelines: Step 1: Use constraint layout in your application. Step 2: Click on the icon shown below or you can also search horizontal or vertical guidelines in palette. Step 3: Select guidelines which you want to use (horizontal or vertical).

How do I change constraint layout?

Open the layout file (activity_main. xml) in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click LinearLayout and then choose Convert layout to ConstraintLayout from the context menu.


1 Answers

Using what azEf recommended works, but you need 2 views instead of 1, and also the barrier will look off at the Layout Preview. The built-in way to do this is app:barrierMargin. Example:

preview of the barrier + barrierMargin usage

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_available"
        app:layout_constraintStart_toStartOf="@id/panelStart" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/iconEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierMargin="10dp"
        android:orientation="vertical"
        app:barrierDirection="end"
        app:constraint_referenced_ids="icon" />

    <TextView
        style="@style/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxLines="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/iconEnd"
        tools:text="Title" />
like image 150
Carsten Hagemann Avatar answered Sep 19 '22 11:09

Carsten Hagemann