我有这样的GridLayout
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:elevation="2dp"
app:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:text="Famous People"
android:textSize="17sp"
android:textStyle="bold" />
<GridLayout
android:id="@+id/llFamousPeople"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:alignmentMode="alignBounds"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:orientation="horizontal"
android:textSize="17sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
我像这样以编程方式在GridLayout中设置值
extra?.famous_people?.forEach {
val textView = TextView(this)
val gridparams = GridLayout.LayoutParams()
gridparams.apply {
height = GridLayout.LayoutParams.WRAP_CONTENT
width = GridLayout.LayoutParams.WRAP_CONTENT
}
textView.layoutParams = gridparams
val marginParams = textView.layoutParams as ViewGroup.MarginLayoutParams
marginParams.bottomMargin = 10
marginParams.topMargin = 10
marginParams.leftMargin = 10
marginParams.rightMargin = 10
textView.setPadding(5, 5, 5, 5)
textView.text = it
llFamousPeople.addView(textView)
}
第三列上的名称已被删除。任何帮助,将不胜感激。谢谢
您可以尝试recyclerview GridLayoutManager
所以有解决方法