AS 2020.3.1
試圖以編程方式設定一個MaterialCheckBox的起始頁邊距,我正以編程方式將其添加到一個布局中。
這是我的布局:
這是我的布局。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"_span>
xmlns:app="http://schemas.android.com/apk/res-auto"。
android:id="@ id/list_item_category_item"
android:layout_width="match_parent"。
android:layout_height="wrap_content"/span>>
<androidx.appcompat.widget.LinearLayoutCompat。
android:id="@ id/llCategories"/span>
android:layout_width="match_parent"。
android:layout_height="wrap_content"。
android:orientation="vertical"。
app:layout_constraintTop_toTopOf="parent"。
app:layout_constraintStart_toStartOf="father" />
</androidx.constraintlayout.widget.ConstraintLayout>
這是我用來設定邊距的代碼片段
這是我用來設定邊距的代碼片段
mapOfCategoryFilters.value.forEach { childCategoryItem ->
val chkChildCategory = MaterialCheckBox(text)
chkChildCategory.setTextColor(ContextCompat.getColor(context, R.color.black)
chkChildCategory.text = childCategoryItem.label
val layoutParams = LinearLayout.LayoutParams(ViewGroup. LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.setMargins(32, 0, 0, 0)
chkChildCategory.layoutParams = layoutParams
llCategories.addView(chkChildCategory)
}
我還嘗試了以下方法,但沒有成功。
val layoutParams = LinearLayout. LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.setMargins(32, 0, 0, 0)
llCategories.addView(chkChildCategory, layoutParams)
我需要通過編程來設定,因為我想根據一個條件來縮進一些。
uj5u.com熱心網友回復:
嘗試
val layoutParams = LinearLayoutCompat.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
你正在使用LinearLayout.LayoutParams。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/329143.html
標籤:

