在ConstraintLayout内为CardView设置约束参数

时间:2018-11-13 21:49:15

标签: android android-layout kotlin android-support-library android-constraintlayout

我需要在Kotlin中以编程方式将某些显示配置应用于卡片视图(因为问题是我无法立即在xml中定义它)

这是xml中的此配置(如果可能,我希望能够以编程方式设置相同)

       <android.support.v7.widget.CardView
            android:id="@+id/resultCard"
            android:layout_width="280dp"
            android:layout_height="340dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.35">

       //some other Views inside this CardView 
     </android.support.v7.widget.CardView>

是否可以通过在Kotlin中进行编码来实现相同的配置?

1 个答案:

答案 0 :(得分:0)

要在Kotlin中实现相同的配置,您可以尝试:

val resultCard = findViewById(R.id.resultCard)
val params = resultCard.layoutParams as ConstraintLayout.LayoutParams
params.apply {
   startToStart = ConstraintLayout.LayoutParams.PARENT_ID
   endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
   topToTop = ConstraintLayout.LayoutParams.PARENT_ID
   bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID
   horizontalBias = .5f
   verticalBias = .35f
}