简体   繁体   中英

How to make Constraint Layout Guideline in center

I have 3 Guideline in Constraint Layout . One is in left with 16dp left margin. Second is in right with 16dp right margin. But I want another Guideline which will be in center . Suppose If I make this guideline center in nexus 5 from Android Stdio XML design panel then in nexus S it's not showing in center. How to solve this?

 <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.blabla.ScrollingActivity">

    <android.support.constraint.Guideline
        android:id="@+id/guideline_end"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="180dp"/>
 </android.support.constraint.ConstraintLayout>

We can set a guideline using percentage by using the tag

app:layout_constraintGuide_percent="0.5"

where 0.5 (50%) being a float value between 0 to 1

In Kotlin:

    // Create constraint layout (maybe you have this already)
    val buttonLayout = ConstraintLayout(context)
    buttonLayout.id = View.generateViewId()

    // Create guideline
    val centerGuideline = Guideline(context)
    centerGuideline.id = View.generateViewId()

    // We want a vertical guideline, 50% across the width
    val centerParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
    centerParams.guidePercent = 0.5f
    centerParams.orientation = ConstraintLayout.LayoutParams.VERTICAL

    // Add guideline to layout.
    buttonLayout.addView(centerGuideline, centerParams)

Now you can update a ConstraintSet and connect to the guideline's id. For example, to have someView start 7dp to the right of center:

    connect(someView.id, ConstraintSet.START, centerGuideline.id, ConstraintSet.START, dpToPx(context, 7))

Where dpToPx is whatever your project uses to convert device units to pixels.

Point to note is the guideline gets rendered erratically on the design screen . Using the app:layout_constraintGuide_percent="0.5" is enough to align it in the middle

<androidx.constraintlayout.widget.Guideline
app:layout_constraintGuide_percent="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM