android - 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 列的名字被删掉了。任何帮助,将不胜感激。谢谢
解决方案
你可以试试 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̥
推荐阅读
- pycharm - PyCharm 不更新自定义配色方案更改
- javascript - “错误:无法修改已提交的 WriteBatch。” 尝试在 firebase 函数中使用批量写入时
- javascript - Javascript 语法混乱
- python - Python 如何销毁垃圾?
- c# - 如何使用异步技巧在 C# 中处理 IO 和 CPU 混合绑定任务?
- corda - build\nodes\runnodes 有问题
- javascript - 如何循环遍历 div?
- azure - 如何在 .net core 3.1 中使用 log4net 将日志保存在 blob 文本文件中
- xaml - Xamarin DataGrid 标题垂直向下
- c# - 保护内存中的对称加密密钥