首页 > 解决方案 > 以编程方式对齐 ConstraintLayout 中的元素

问题描述

我在 Kotlin 中创建 a TextView, anEditText和 aButton在 a ConstraintLayout,我想让它们水平居中,垂直居中,但无论我尝试什么,它们仍然在左上角。

现在我有:

    val layout = findViewById<ConstraintLayout>(R.id.mainLayout)
    layout.removeAllViews()

    //Here i try to set my alignments
    var layoutParams = ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT)
    layoutParams.leftToLeft = ConstraintLayout.LayoutParams.MATCH_PARENT
    layoutParams.rightToRight = ConstraintLayout.LayoutParams.MATCH_PARENT
    layoutParams.topToTop = ConstraintLayout.LayoutParams.MATCH_PARENT
    layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.MATCH_PARENT


    layoutParams.verticalBias = 0.05f
    val tv_dynamic = TextView(this)
    tv_dynamic.text = "Text :"
    tv_dynamic.layoutParams = layoutParams
    layout?.addView(tv_dynamic)

    layoutParams.verticalBias = 0.15f
    val et_dynamic = EditText(this)
    et_dynamic.setText("...")
    et_dynamic.layoutParams = layoutParams
    layout?.addView(et_dynamic)

    layoutParams.verticalBias = 0.3f
    val b_dynamic = Button(this)
    b_dynamic.text = "Ok"
    b_dynamic.setOnClickListener(View.OnClickListener(){ changenamevalidate(et_dynamic.text.toString())} )
    b_dynamic.layoutParams  = layoutParams
    layout?.addView(b_dynamic)

我的设置基于我已经拥有的 xml TextView:

<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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:id="@+id/TextViewTeam"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Team Name!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.05" />

</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidxmlkotlinandroid-constraintlayout

解决方案


android:layoutgravity="center_horizontal"向您的 ConstraintLayout添加一个属性,如下所示:

    <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:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<TextView
    android:id="@+id/TextViewTeam"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Team Name!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.05" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读