【发布时间】:2017-06-02 07:24:39
【问题描述】:
我正在尝试设计以下布局
<android.support.constraint.ConstraintLayout
android:id="@+id/before_breakfast_option"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/diabetes_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="water"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/black"
app:layout_constraintBaseline_toBaselineOf="@+id/toogle_diabeties"
app:layout_constraintLeft_toLeftOf="parent"/>
<TextView
android:textColor="@color/black"
android:text="almonds"
app:layout_constraintTop_toTopOf="parent"
android:id="@+id/toogle_diabeties"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.constraint.ConstraintLayout>
使用以下代码:
var textView= TextView(this@DietStepFive)
textView.id=100
textView.text="water"
textView.background=ContextCompat.getDrawable(this@DietStepFive,R.drawable.rectangle_diet)
textView.setTextColor(ContextCompat.getColor(this@DietStepFive,R.color.black))
var textView1= TextView(this@DietStepFive)
textView1.id=101
textView1.text="almonds"
textView1.background=ContextCompat.getDrawable(this@DietStepFive,R.drawable.rectangle_diet)
textView1.setTextColor(ContextCompat.getColor(this@DietStepFive,R.color.black))
var constraintset= ConstraintSet()
constraintset.clone(before_breakfast_option)
//left to left of
constraintset.connect(textView.id,ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0)
//baseline
constraintset.connect(textView.id,ConstraintSet.BASELINE,textView1.id,ConstraintSet.BASELINE,0)
//right to right of
constraintset.connect(textView1.id,ConstraintSet.RIGHT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0)
//top to top of
constraintset.connect(textView1.id,ConstraintSet.TOP,ConstraintSet.PARENT_ID,ConstraintSet.TOP,0)
constraintset.applyTo(before_breakfast_option)
before_breakfast_option.addView(textView)
before_breakfast_option.addView(textView1)
但是 XML 代码给我的布局有两个 textview 一个是左侧,一个是右侧,但是 kotlin 代码给了我两个 textview 在左侧重叠。为什么?
出了什么问题?有铅吗?
【问题讨论】:
-
向两个TextView添加布局重力
-
我已将左侧重力添加到 textview 并将右侧添加到 textview1 但没有任何反应@TerrilThomas
-
layout_gravity 而不仅仅是重力
-
我无法添加 layout_gravity 属性,因为为此我需要将 textview 包装在宽度为 match_parent 的线性布局中,但我不想那样做。 @TerrilThomas
-
此外,它解决了这个问题,但一般来说,我想在布局中添加尽可能多的视图,直到没有空间为止。对于那个问题,我无法设置重力。 @TerrilThomas
标签: android view kotlin android-constraintlayout