为什么动态按钮不尊重Kotlin的高度?

时间:2019-06-06 05:29:13

标签: android kotlin layout dynamic

在下面KotlinAndroid Studio的简化示例中,我在constraint layout空中使用了XML

在我动态创建带有背景图像的textview之后,又创建了一个大小相同且带有集中文本的button。我还对与按钮,文本和父项有关的约束进行了编程(约束布局)。

我手机上的显示屏顽固地拉伸了按钮,占据了屏幕的下半部分。

我做错了什么?

我唯一活动(进口除外)的代码是

var idGlob:Int = 100
fun incId():Int {
   idGlob++
   return idGlob  
}

class MainActivity : AppCompatActivity() {
    lateinit var newView:ImageView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val newText: TextView = TextView(this)
        myLayout.addView(newText)
        newText.setText("XO")
        newText.setTextColor(Color.WHITE);
        newText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28F);
        newText.layoutParams.height = 200
        newText.layoutParams.width = 400
                  // XML Vector Image from a SVG file
        newText.setBackgroundResource(R.drawable.ic_square) 
        newText.gravity = Gravity.CENTER
        newText.id = incId()  // Custom function to generate unique ID
        val c = ConstraintSet()
        c.clone(myLayout)    
        c.connect(newText.id,ConstraintSet.START,   
              ConstraintSet.PARENT_ID,ConstraintSet.START,0)
        c.connect(newText.id,ConstraintSet.END,
              ConstraintSet.PARENT_ID,ConstraintSet.END,0)
        c.connect(newText.id,ConstraintSet.TOP,
              ConstraintSet.PARENT_ID,ConstraintSet.TOP,0)       
        c.connect(newText.id,ConstraintSet.BOTTOM,
              ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0)
        val newBut: Button  = Button(this)
        myLayout.addView(newBut)
        newBut.height = 200  // I also tried newBut.layoutParams.height
        newBut.width = 400   // I also tried newBut.layoutParams.width
        newBut.id = incId() // Custom function to generate unique ID
        newBut.setBackgroundColor(Color.MAGENTA)
        newBut.text = "CENTER"
        newBut.textAlignment = ALIGN_CENTER
        c.connect(newBut.id,ConstraintSet.START,
            newText.id,ConstraintSet.START,0)
        c.connect(newBut.id,ConstraintSet.END,
            newText.id,ConstraintSet.END,0)
        c.connect(newBut.id,ConstraintSet.TOP,   // If I remove this line
            newText.id,ConstraintSet.BOTTOM,0)
        c.connect(newBut.id,ConstraintSet.BOTTOM, // or this one(see PS3)
            ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0)
        c.applyTo(myLayout)

    }
}

手机中的结果是

Cell phone display<code>enter code here</code>

更新:为了显示静态解决方案的动态等效项不起作用,具有相同值的静态解决方案可以正常工作:

<androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/myLayout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="400px"
            android:layout_height="200px"
            android:text="XO"
            android:background="@drawable/ic_square"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" android:id="@+id/textView"/>
    <Button
            android:text="Center"
            android:layout_width="400px"
            android:layout_height="200px"
            android:id="@+id/button"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

PS 1 :我的布局使用Constraint Layout。所以我尝试了

newBut.setLayoutParams(ConstraintLayout.LayoutParams(400, 200))

不幸的是,它也不起作用!

PS 2 :我也尝试过

newBut.maxHeight = 200

什么都没有改变。

PS 3 :如果我从代码中删除了两个最终connect调用之一(按钮顶部或底部),则Center button消失了!

PS 4 :如果我从connect中删除所有Center button个呼叫并添加

newBut.y = 1200F
newBut.x = 300F

大小就像一个吊饰,但是我需要使用坐标而不是约束。

1 个答案:

答案 0 :(得分:2)

在这里,您在问题c.connect(newBut.id,ConstraintSet.BOTTOM, // or this one(see PS3) ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0)中的问题是将按钮约束底部与父对象对齐,这就是为什么它要拉伸而不是使用      val newText:TextView = TextView(this)

    myLayout.addView(newText)
    newText.setText("XO")
    newText.setTextColor(Color.WHITE);
    newText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28F);
    newText.layoutParams.height = 200
    newText.layoutParams.width = 400
    // XML Vector Image from a SVG file
    newText.setBackgroundResource(R.drawable.ic_square)
    newText.gravity = Gravity.CENTER
    newText.id = incId()  // Custom function to generate unique ID
    val c = ConstraintSet()
    c.clone(myLayout)
    c.connect(newText.id,ConstraintSet.START,
            ConstraintSet.PARENT_ID,ConstraintSet.START,0)
    c.connect(newText.id,ConstraintSet.END,
            ConstraintSet.PARENT_ID,ConstraintSet.END,0)
    c.connect(newText.id,ConstraintSet.TOP,
            ConstraintSet.PARENT_ID,ConstraintSet.TOP,0)
    c.connect(newText.id,ConstraintSet.BOTTOM,
            ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM,0)
    val newBut: Button  = Button(this)
    myLayout.addView(newBut)
    newBut.height = 200  // I also tried newBut.layoutParams.height
    newBut.width = 400   // I also tried newBut.layoutParams.width
    newBut.id = incId() // Custom function to generate unique ID
    newBut.setBackgroundColor(Color.GRAY)
    newBut.text = "CENTER"
    newBut.textAlignment = ALIGN_CENTER

    c.connect(newBut.id,ConstraintSet.START,
            newText.id,ConstraintSet.START,0)
    c.connect(newBut.id,ConstraintSet.END,
            newText.id,ConstraintSet.END,0)
    c.connect(newBut.id,ConstraintSet.TOP,   // If I remove this line
            newText.id,ConstraintSet.BOTTOM,0)
    c.constrainHeight(newBut.id,200) ***// set button height here***
 /*   c.connect(newBut.id,ConstraintSet.BOTTOM, // or this one(see PS3)
            newBut.id,ConstraintSet.BOTTOM,0)*/
    c.applyTo(myLayout)
相关问题