【问题标题】:TextViews inside GridLayout are being cut off on the right side AndroidGridLayout 中的 TextViews 在 Android 右侧被截断
【发布时间】:2020-08-09 02:51:56
【问题描述】:

我有一个这样的 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 列的名字被删掉了。任何帮助,将不胜感激。谢谢

【问题讨论】:

    标签: android kotlin textview android-gridlayout


    【解决方案1】:

    你可以试试 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̥
    

    【讨论】:

      【解决方案2】:

      所以有办法解决这个问题

      First

      在 Gridlayout 中创建 2 列,它将为所有名称腾出足够的空间。 android:columnCount="3"

      Second 制作文本 2 衬里并将其重力设置在中心。如果你会让 minLine 1 和 maxLine 2,所有文本都不会对齐,所以设置 minline 2 & maxline 2,它将使所有文本覆盖的空间等于两行。

                    <TextView                         
                      android:minLines="2"
                      android:maxLines="2"/>
      

      Third

      将 Recyclerview 与 GridLayoutManager 方法一起使用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-31
        • 1970-01-01
        • 1970-01-01
        • 2014-12-25
        • 2013-10-13
        • 2016-06-22
        • 1970-01-01
        • 2017-04-22
        相关资源
        最近更新 更多