You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android中用TableLayout创建动态表格,能否在XML布局中设置列属性?

在Android中实现带XML预定义样式的动态表格

一、TableLayout的可行实现方式

TableLayout完全支持在XML中预设列的样式,无需每次在Java/Kotlin代码中重复设置文本大小、颜色等属性,核心思路是复用行布局模板

1. 定义可复用的TableRow布局

创建一个独立的行布局文件(比如table_row_template.xml),在其中给每个TextView预设好样式属性:

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical">

    <TextView
        android:id="@+id/tv_col_one"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textSize="14sp"
        android:textColor="@color/primary_text"
        android:paddingVertical="10dp"
        android:paddingHorizontal="8dp"/>

    <TextView
        android:id="@+id/tv_col_two"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:textSize="14sp"
        android:textColor="@color/secondary_text"
        android:paddingVertical="10dp"
        android:paddingHorizontal="8dp"/>

    <!-- 根据需求添加更多列 -->
</TableRow>

2. 代码中动态加载模板并填充数据

在Activity/Fragment中,只需加载这个布局模板,设置文本内容即可,样式会自动继承XML中的配置:

val tableLayout = findViewById<TableLayout>(R.id.main_table)
val dataList = getTableData() // 你的数据源

for (data in dataList) {
    val row = LayoutInflater.from(this)
        .inflate(R.layout.table_row_template, tableLayout, false) as TableRow
    
    row.findViewById<TextView>(R.id.tv_col_one).text = data.columnOne
    row.findViewById<TextView>(R.id.tv_col_two).text = data.columnTwo
    
    tableLayout.addView(row)
}

3. 进阶:用Style统一管理样式

如果多列样式重复,可以在res/values/styles.xml中定义通用样式,便于统一修改:

<style name="TableColumnStyle">
    <item name="android:textSize">14sp</item>
    <item name="android:paddingVertical">10dp</item>
    <item name="android:paddingHorizontal">8dp</item>
</style>

然后在TableRow布局中引用:

<TextView
    android:id="@+id/tv_col_one"
    android:layout_width="0dp"
    android:layout_weight="1"
    style="@style/TableColumnStyle"
    android:textColor="@color/primary_text"/>

二、当TableLayout满足不了需求时:RecyclerView构建表格

如果表格数据量较大(比如超过50行),或者需要滑动流畅性、视图复用优化,推荐用RecyclerView实现:

核心实现思路

  • 用垂直方向的LinearLayoutManager,每个列表项对应表格的一行
  • 每行布局和TableLayout的行模板完全一致,依旧在XML中预设样式
  • 自定义Adapter绑定数据,仅需设置文本内容

示例Adapter代码:

class TableAdapter(private val dataList: List<TableData>) : RecyclerView.Adapter<TableAdapter.RowViewHolder>() {

    inner class RowViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val colOneTv: TextView = itemView.findViewById(R.id.tv_col_one)
        val colTwoTv: TextView = itemView.findViewById(R.id.tv_col_two)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RowViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.table_row_template, parent, false)
        return RowViewHolder(view)
    }

    override fun onBindViewHolder(holder: RowViewHolder, position: Int) {
        val data = dataList[position]
        holder.colOneTv.text = data.columnOne
        holder.colTwoTv.text = data.columnTwo
    }

    override fun getItemCount(): Int = dataList.size
}

Activity中初始化RecyclerView:

val recyclerView = findViewById<RecyclerView>(R.id.table_recycler)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = TableAdapter(dataList)

总结

  • TableLayout适合小数据量、简单场景,通过复用XML行模板就能实现样式预定义,无需在代码中重复设置属性
  • RecyclerView适合大数据量、高性能需求场景,同样支持XML预定义样式,且自带视图复用机制,滑动更流畅

内容的提问来源于stack exchange,提问作者Sunil Agarwal

火山引擎 最新活动