首页 > 解决方案 > GridLayout 中的 TextViews 在 Android 右侧被截断

问题描述

我有一个像这样的 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)
        }

结果视图如下所示 在此处输入图像描述

第 3 列的名字被删掉了。任何帮助,将不胜感激。谢谢

标签: androidkotlintextviewandroid-gridlayout

解决方案


你可以试试 recyclerview GridLayoutManager

 <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" />

                       <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/rc_txt_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                     app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
                       app:spanCount="3"

           />
                    </LinearLayout>
                </com.google.android.material.card.MaterialCardView>l̥

推荐阅读