试图以编程方式将视图添加到 Kotlin 中的 ContraintLayout 的奇怪行为
Strange behavior trying to add views programmatically to a ContraintLayout in Kotlin
我是 Android 的 iOS 开发人员背景,在以编程方式添加视图时,我很难让 ConstraintLayout 正常工作。
我有一个非常简单的 activity,其中包含一个 ConstraintLayout 和一个按钮,我将其放置在 Android Studio 设计 window 中。当按下按钮时,我添加了一个 textView 和另一个按钮。我试图限制它们,使它们与 ConstraintLayout 顶部的距离相同,并且 textView 的前缘被限制在 ConstraintLayout 的左侧,按钮被限制在 ConstraintLayout 的右侧。
我的XML如下:
<android.support.constraint.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/parentViewConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
tools:context=".MainActivity">
<Button
android:id="@+id/addWidgetButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="@font/muli_bold"
android:text="Add New Widget"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
还有问题中的 Kotlin
addWidgetButton.setOnClickListener() {
val textView = TextView(this)
textView.setTextColor(Color.BLACK)
textView.setTextSize(20f)
textView.setText("Text message for testing")
val newButton = Button(this)
newButton.setText("Push me")
parentViewConstraintLayout.addView(textView)
parentViewConstraintLayout.addView(newButton)
val constraintSet = ConstraintSet()
constraintSet.clone(parentViewConstraintLayout)
constraintSet.connect(textView.id, ConstraintSet.TOP, parentViewConstraintLayout.id, ConstraintSet.TOP, 10)
constraintSet.connect(textView.id, ConstraintSet.START, parentViewConstraintLayout.id, ConstraintSet.START, 10)
constraintSet.connect(newButton.id, ConstraintSet.END, parentViewConstraintLayout.id, ConstraintSet.END, 30)
constraintSet.connect(newButton.id, ConstraintSet.TOP, parentViewConstraintLayout.id, ConstraintSet.TOP, 50)
constraintSet.applyTo(parentViewConstraintLayout)
}
当按下按钮时,我得到的不是预期的行为:
如果我注释掉一些代码以便我只添加一个视图(按钮或 textView)它可以正常工作。他们之间有一些我不明白的奇怪的互动。
ConstraintLayout
约束与资源 ID 一起使用。我看到您在哪里创建 TextView
和 Button
,但我没有看到您在哪里为这些视图设置 ID。也许一些 Kotlin 魔法?
对于API 17+,您可以使用View.generateViewId()
to generate the id and View.setId()
to set it. You can then refer to the ids in the ConstraintSet
. Right now they are both View.NO_ID
.。
我还要补充一点,如果您想将父级作为约束引用,它是 ConstraintSet.PARENT_ID
。
I am trying to constrain these such that they are the same distance from the top of the ConstraintLayout, and the leading edge of the textView is constrained to the left side of the ConstraintLayout, and the button is constrained to the right side of the ConstraintLayout.
好的,如果我没记错的话……你想要的是并排放置一个 TextView 和一个 Button,对吗?
| [TextView] [Button]|
或
| [TextView][Button] |
或
| [TextView ][Button ]|
或
?
根据您的需要,约束必须不同。
此外,您说:
they are the same distance from the top of the ConstraintLayout
这意味着如果你一直按下按钮,它们将有效地位于前一个之上,这是故意的吗?
或者您的意思是“我希望它们低于前一个,尊重“上边距”,以便每个元素之间的垂直 space 相同?”
无论如何,您应该(因为您使用的是 CL 1.0.2)正确指定这些视图的所有属性。
想想 AutoLayout,除非 View 具有固有大小,否则您需要先固定 start/end/top/bottom,然后算法才能确定视图必须放在哪里。
无论如何回到你的样本......
我加了
textView.id = View.generateViewId()
和
newButton.id = View.generateViewId()
和 运行 您的代码并且它可以工作……但是 button/text 的每个新“对”都添加在顶部。
如果你需要做|[Tv ][btn]|
你可以简单地添加这两个:
textView.layoutParams = Constraints.LayoutParams(ConstraintSet.MATCH_CONSTRAINT, ConstraintSet.WRAP_CONTENT)
和
constraintSet.connect(textView.id, ConstraintSet.END, newButton.id, ConstraintSet.START, 10)
那应该给你。
我是 Android 的 iOS 开发人员背景,在以编程方式添加视图时,我很难让 ConstraintLayout 正常工作。
我有一个非常简单的 activity,其中包含一个 ConstraintLayout 和一个按钮,我将其放置在 Android Studio 设计 window 中。当按下按钮时,我添加了一个 textView 和另一个按钮。我试图限制它们,使它们与 ConstraintLayout 顶部的距离相同,并且 textView 的前缘被限制在 ConstraintLayout 的左侧,按钮被限制在 ConstraintLayout 的右侧。
我的XML如下:
<android.support.constraint.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/parentViewConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_orange_dark"
tools:context=".MainActivity">
<Button
android:id="@+id/addWidgetButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="@font/muli_bold"
android:text="Add New Widget"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
还有问题中的 Kotlin
addWidgetButton.setOnClickListener() {
val textView = TextView(this)
textView.setTextColor(Color.BLACK)
textView.setTextSize(20f)
textView.setText("Text message for testing")
val newButton = Button(this)
newButton.setText("Push me")
parentViewConstraintLayout.addView(textView)
parentViewConstraintLayout.addView(newButton)
val constraintSet = ConstraintSet()
constraintSet.clone(parentViewConstraintLayout)
constraintSet.connect(textView.id, ConstraintSet.TOP, parentViewConstraintLayout.id, ConstraintSet.TOP, 10)
constraintSet.connect(textView.id, ConstraintSet.START, parentViewConstraintLayout.id, ConstraintSet.START, 10)
constraintSet.connect(newButton.id, ConstraintSet.END, parentViewConstraintLayout.id, ConstraintSet.END, 30)
constraintSet.connect(newButton.id, ConstraintSet.TOP, parentViewConstraintLayout.id, ConstraintSet.TOP, 50)
constraintSet.applyTo(parentViewConstraintLayout)
}
当按下按钮时,我得到的不是预期的行为:
如果我注释掉一些代码以便我只添加一个视图(按钮或 textView)它可以正常工作。他们之间有一些我不明白的奇怪的互动。
ConstraintLayout
约束与资源 ID 一起使用。我看到您在哪里创建 TextView
和 Button
,但我没有看到您在哪里为这些视图设置 ID。也许一些 Kotlin 魔法?
对于API 17+,您可以使用View.generateViewId()
to generate the id and View.setId()
to set it. You can then refer to the ids in the ConstraintSet
. Right now they are both View.NO_ID
.。
我还要补充一点,如果您想将父级作为约束引用,它是 ConstraintSet.PARENT_ID
。
I am trying to constrain these such that they are the same distance from the top of the ConstraintLayout, and the leading edge of the textView is constrained to the left side of the ConstraintLayout, and the button is constrained to the right side of the ConstraintLayout.
好的,如果我没记错的话……你想要的是并排放置一个 TextView 和一个 Button,对吗?
| [TextView] [Button]|
或
| [TextView][Button] |
或
| [TextView ][Button ]|
或
?
根据您的需要,约束必须不同。
此外,您说:
they are the same distance from the top of the ConstraintLayout
这意味着如果你一直按下按钮,它们将有效地位于前一个之上,这是故意的吗?
或者您的意思是“我希望它们低于前一个,尊重“上边距”,以便每个元素之间的垂直 space 相同?”
无论如何,您应该(因为您使用的是 CL 1.0.2)正确指定这些视图的所有属性。
想想 AutoLayout,除非 View 具有固有大小,否则您需要先固定 start/end/top/bottom,然后算法才能确定视图必须放在哪里。
无论如何回到你的样本......
我加了
textView.id = View.generateViewId()
和
newButton.id = View.generateViewId()
和 运行 您的代码并且它可以工作……但是 button/text 的每个新“对”都添加在顶部。
如果你需要做|[Tv ][btn]|
你可以简单地添加这两个:
textView.layoutParams = Constraints.LayoutParams(ConstraintSet.MATCH_CONSTRAINT, ConstraintSet.WRAP_CONTENT)
和
constraintSet.connect(textView.id, ConstraintSet.END, newButton.id, ConstraintSet.START, 10)
那应该给你。