android - 以编程方式对齐 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>
解决方案
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>
推荐阅读
- swift - UITableView reloadRows 有时会崩溃
- mysql - 我想在 yii2 框架中选择并显示两个日期之间的月份和日期
- maven - Jenkins 无法构建 maven 和 gradle 项目
- python - 如何在 python Ubuntu 中将 PPT 文件转换为 PDF?
- javascript - Vue 在 v-for 循环内动态添加一个组件
- python - 根据其他列中的值替换值,如果条件不满足则保持原样
- c++ - 抛出多个异常时,catch 块执行的顺序是什么,为什么?
- kubernetes - 为什么内部通信的通信时间(速度)比以前慢了一点?
- javascript - 如何从控制器渲染两种类型的数据以在 node.js 中查看?
- ajax - 在 ajax 发布请求中,我的应用被 CORS 策略错误阻止