在 Kotlin Anko 中使用 ConstraintLayout
Using ConstraintLayout with Kotlin Anko
我正在尝试 ContraintLayout support in Anko 和 运行 遇到一个问题,我无法让最简单的示例正常工作。
我在 constraintLayout 中将两个按钮水平放置在屏幕顶部,充当链条,使它们水平居中。
我做的第一件事是使用良好的旧 xml 布局和 Android Studio,这样我就可以查看生成的代码。
这是我的非 Anko xml 布局
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button4"
/>
这会生成如下所示的正确布局
现在下面是我认为应该是产生相同结果的等效 Kotlin Anko 代码。
constraintLayout {
id = R.id.constraintRoot
val first : Button = button("First"){
id = R.id.firstButton
width = wrapContent
height = wrapContent
}.lparams{
//topToTop = ConstraintSet.PARENT_ID
startToStart = PARENT_ID
endToStart = R.id.secondButton
horizontalChainStyle = spread
editorAbsoluteY = 16
horizontalBias = 0.5f
}
val second : Button = button("Second"){
id = R.id.secondButton
width = wrapContent
height = wrapContent
}.lparams{
startToEnd = R.id.firstButton
endToEnd = PARENT_ID
editorAbsoluteY = 16
horizontalBias = 0.5f
}
}
但由于某种原因,这会产生以下结果。
如您所见,按钮在视图中没有均匀分布并且水平居中。
有什么我遗漏的吗?或者 Anko 的 ConstraintLayout 还没有准备好用于生产吗?
谢谢!
所以我最终使用了一个指南来给我想要的结果。
constraintLayout {
id = R.id.constraintRoot
val first : Button = button("First"){
id = R.id.firstButton
width = wrapContent
height = wrapContent
}.lparams{
endToStart = R.id.verticalGuideline
startToStart = ConstraintSet.PARENT_ID
}
val gl : Guideline = guideline {
id = R.id.verticalGuideline
}.lparams {
orientation = ConstraintLayout.LayoutParams.VERTICAL
guidePercent = 0.5f
}
val second : Button = button("Second"){
id = R.id.secondButton
width = wrapContent
height = wrapContent
}.lparams{
startToEnd = R.id.verticalGuideline
endToEnd = ConstraintSet.PARENT_ID
}
}
按钮的宽度和高度应为 lparams(在 lparams 主体中或作为 lparams() 的参数。我不确定这是否会解决问题。
我正在尝试 ContraintLayout support in Anko 和 运行 遇到一个问题,我无法让最简单的示例正常工作。
我在 constraintLayout 中将两个按钮水平放置在屏幕顶部,充当链条,使它们水平居中。
我做的第一件事是使用良好的旧 xml 布局和 Android Studio,这样我就可以查看生成的代码。
这是我的非 Anko xml 布局
<?xml version="1.0" encoding="utf-8"?>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
app:layout_constraintEnd_toStartOf="@+id/button5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/button4"
/>
这会生成如下所示的正确布局
现在下面是我认为应该是产生相同结果的等效 Kotlin Anko 代码。
constraintLayout {
id = R.id.constraintRoot
val first : Button = button("First"){
id = R.id.firstButton
width = wrapContent
height = wrapContent
}.lparams{
//topToTop = ConstraintSet.PARENT_ID
startToStart = PARENT_ID
endToStart = R.id.secondButton
horizontalChainStyle = spread
editorAbsoluteY = 16
horizontalBias = 0.5f
}
val second : Button = button("Second"){
id = R.id.secondButton
width = wrapContent
height = wrapContent
}.lparams{
startToEnd = R.id.firstButton
endToEnd = PARENT_ID
editorAbsoluteY = 16
horizontalBias = 0.5f
}
}
但由于某种原因,这会产生以下结果。
如您所见,按钮在视图中没有均匀分布并且水平居中。
有什么我遗漏的吗?或者 Anko 的 ConstraintLayout 还没有准备好用于生产吗?
谢谢!
所以我最终使用了一个指南来给我想要的结果。
constraintLayout {
id = R.id.constraintRoot
val first : Button = button("First"){
id = R.id.firstButton
width = wrapContent
height = wrapContent
}.lparams{
endToStart = R.id.verticalGuideline
startToStart = ConstraintSet.PARENT_ID
}
val gl : Guideline = guideline {
id = R.id.verticalGuideline
}.lparams {
orientation = ConstraintLayout.LayoutParams.VERTICAL
guidePercent = 0.5f
}
val second : Button = button("Second"){
id = R.id.secondButton
width = wrapContent
height = wrapContent
}.lparams{
startToEnd = R.id.verticalGuideline
endToEnd = ConstraintSet.PARENT_ID
}
}
按钮的宽度和高度应为 lparams(在 lparams 主体中或作为 lparams() 的参数。我不确定这是否会解决问题。