【问题标题】:Why a dynamic button does not respect the height in Kotlin?为什么动态按钮不尊重 Kotlin 中的高度?
【发布时间】:2019-10-21 14:32:29
【问题描述】:

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

在我动态创建了一个带有背景图像的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)

    }
}

手机里的结果是

更新:为了表明静态解决方案的动态等效项不起作用,具有相同值的静态解决方案可以完美运行:

<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:如果我从Center button 中删除所有connect 调用并添加

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

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

【问题讨论】:

  • 这不是为您的按钮设置宽度和高度的方法。我相信你应该使用布局参数
  • 我的布局使用Constraint Layout。所以我试过newBut.setLayoutParams(ConstraintLayout.LayoutParams(400, 200)),但也没有用!
  • 请不要仅仅因为你使用它就用 android-studio 标签标记问题:Android Studio 标签应该只在你对 IDE 本身有疑问时使用,而不是你编写的任何代码(或者想写)在里面。
  • 我会删除它。

标签: android kotlin layout dynamic


【解决方案1】:

这里是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)

【讨论】:

  • 这很有趣,因为我的代码是静态的(XML),但不是动态的。但是,您的解决方案有效!只是在我的代码中添加c.constrainHeight(newBut.id,200)。我的代码中没有connect 更改。我不知道为什么!如果我将您的行替换为newBut.height = 200,它将失败并再次拉伸矩形。
猜你喜欢
  • 2017-11-13
  • 2022-10-07
  • 1970-01-01
  • 2016-09-06
  • 1970-01-01
  • 2015-08-05
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
相关资源
最近更新 更多