在ConstraintLayout中为CardView设置约束参数

问题描述 投票:0回答:1

我需要在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中编码来实现相同的配置?

android android-layout kotlin android-support-library android-constraintlayout
1个回答
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
}
© www.soinside.com 2019 - 2024. All rights reserved.