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" />
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).
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).
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.
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:
<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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With