2017-06-02 87 views
0

我嘗試使用下面的代碼設計如下佈局如何以編程方式在約束佈局中添加視圖?

<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([email protected]) 
        textView.id=100 
        textView.text="water" 
        textView.background=ContextCompat.getDrawable([email protected],R.drawable.rectangle_diet) 
        textView.setTextColor(ContextCompat.getColor([email protected],R.color.black)) 

        var textView1= TextView([email protected]) 
        textView1.id=101 
        textView1.text="almonds" 
        textView1.background=ContextCompat.getDrawable([email protected],R.drawable.rectangle_diet) 
        textView1.setTextColor(ContextCompat.getColor([email protected],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重疊在左側。爲什麼?

什麼是錯誤的?任何主角?

+0

添加到這兩個TextView的 –

+0

我加入左重力的TextView和向右textview1但沒有任何反應@TerrilThomas –

+0

layout_gravity而佈局重心不只是重力 –

回答

-2

嘗試更換app:layout_constraintRight_toRightOf="parent"

app:layout_constraintRight_toRightOf="@+id/toogle_diabeties"

+0

我的xml代碼工作正常,不是kotlin代碼。 –

1

TextView S添加到佈局,然後將它們連接起來就像你一樣建立XML時。你添加了視圖,然後連接它們。

移動

before_breakfast_option.addView(textView) 
before_breakfast_option.addView(textView1) 

var constraintset= ConstraintSet() 

,一切都應該工作。

+0

它不按預期工作。結果和以前一樣@Cheticamp –

+0

@AnkurKhandelwal你絕對需要這樣做,但這可能不是你唯一的問題。我對Kotlin不熟悉,所以如果是Kotlin問題,我無法幫助。我想知道'before_breakfast_option'指的是什麼,你是否真的得到了你的約束。這是一個[快速要點](https://gist.github.com/Cheticamp/4e74a6210728b952cb35e7e2a5656b91)Java代碼,你可以看看。它可以幫助你找出你剩下的問題。 – Cheticamp

+0

@AnkurKhandelwal我做了上述要點轉換爲Kotlin,併成功運行結果。這是工作Java代碼的[Kotlin版本](https://gist.github.com/Cheticamp/e0af76d73f7a10367efe4dde4ff2f877)。我希望這有幫助。 – Cheticamp

0

也許其他人將在未來使用它。完美的睡眠和完成的工作。 我使用了錯誤的約束。

取而代之的是

   //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) 

使用本

//left to right of 
        constraintset.connect(textView1.id,ConstraintSet.LEFT,textView.id,ConstraintSet.RIGHT,10) 
        //baseline 
        constraintset.connect(textView1.id,ConstraintSet.BASELINE,textView.id,ConstraintSet.BASELINE,0) 
相關問題