Guideline
class Guideline : View
Utility class representing a Guideline helper object for ConstraintLayout. Helper objects are not displayed on device (they are marked as View.GONE) and are only used for layout purposes. They only work within a ConstraintLayout.
A Guideline can be either horizontal or vertical:
- Vertical Guidelines have a width of zero and the height of their
ConstraintLayoutparent - Horizontal Guidelines have a height of zero and the width of their
ConstraintLayoutparent
Positioning a Guideline is possible in three different ways:
- specifying a fixed distance from the left or the top of a layout (
layout_constraintGuide_begin) - specifying a fixed distance from the right or the bottom of a layout (
layout_constraintGuide_end) - specifying a percentage of the width or the height of a layout (
layout_constraintGuide_percent)
Widgets can then be constrained to a Guideline, allowing multiple widgets to be positioned easily from one Guideline, or allowing reactive layout behavior by using percent positioning.
See the list of attributes in androidx.constraintlayout.widget.ConstraintLayout.LayoutParams to set a Guideline in XML, as well as the corresponding setGuidelineBegin, setGuidelineEnd and setGuidelinePercent functions in ConstraintSet.
Example of a Button constrained to a vertical Guideline:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.Guideline android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/guideline" app:layout_constraintGuide_begin="100dp" android:orientation="vertical"/> <Button android:text="Button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" app:layout_constraintLeft_toLeftOf="@+id/guideline" android:layout_marginTop="16dp" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
Summary
Public constructors |
|---|
Guideline(context: Context!, attrs: AttributeSet!) |
Guideline(context: Context!, attrs: AttributeSet!, defStyleAttr: Int) |
Guideline( |
Public functions |
|
|---|---|
Unit |
We are overriding draw and not calling super.draw() here because Helper objects are not displayed on device. |
Unit |
setFilterRedundantCalls(filter: Boolean)filter redundant calls to setGuidelineBegin, setGuidelineEnd &setGuidelinePercent. |
Unit |
setGuidelineBegin(margin: Int)Set the guideline's distance from the top or left edge. |
Unit |
setGuidelineEnd(margin: Int)Set a guideline's distance to end. |
Unit |
setGuidelinePercent(ratio: Float)Set a Guideline's percent. |
Unit |
setVisibility(visibility: Int) |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public functions
draw
fun draw(canvas: Canvas): Unit
We are overriding draw and not calling super.draw() here because Helper objects are not displayed on device.
setFilterRedundantCalls
fun setFilterRedundantCalls(filter: Boolean): Unit
filter redundant calls to setGuidelineBegin, setGuidelineEnd &setGuidelinePercent. By default calling setGuidelineStart,setGuideLineEnd and setGuidelinePercent will do nothing if the value is the same as the current value. This can disable that behaviour and call setLayoutParams(..) while will call requestLayout
| Parameters | |
|---|---|
filter: Boolean |
default true set false to always generate a setLayoutParams |
setGuidelineBegin
fun setGuidelineBegin(margin: Int): Unit
Set the guideline's distance from the top or left edge.
| Parameters | |
|---|---|
margin: Int |
the distance to the top or left edge |
setGuidelineEnd
fun setGuidelineEnd(margin: Int): Unit
Set a guideline's distance to end.
| Parameters | |
|---|---|
margin: Int |
the margin to the right or bottom side of container |
setGuidelinePercent
fun setGuidelinePercent(ratio: Float): Unit
Set a Guideline's percent.
| Parameters | |
|---|---|
ratio: Float |
the ratio between the gap on the left and right 0.0 is top/left 0.5 is middle |