android - 在表布局中克隆行

标签 android android-layout android-tablelayout

我有以下 my_layout.xml 布局

<TableLayout android:id="@+id/tlTable" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="1,2"  android:layout_marginTop="20dp"  >
        <TableRow android:id="@+id/trRow" android:layout_width="wrap_content" android:layout_height="wrap_content">
            <TextView android:id="@+id/tv1" style="@style/showtext_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row1" android:layout_marginRight="5dp"/>
            <ProgressBar android:id="@+id/p2" style="?android:attr/progressBarStyleHorizontal" android:progressDrawable="@drawable/greenprogress"  android:layout_width="wrap_content" android:layout_height="wrap_content"  android:layout_marginRight="5dp" />

            <TextView android:id="@+id/tv3"  android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="0"   />
    </TableRow >
  </TableLayout>

我想动态创建 10 行,其中所有行都是第一行的克隆,除了每行的第一个 TextView (即 tv1)的文本要更改为具有行号。所以它将是 Row1, Row2, Row3.....Row10

我怎样才能做到这一点?

最佳答案

将此代码放入单独的布局中:

<TextView android:id="@+id/tv1" style="@style/showtext_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Row1" android:layout_marginRight="5dp"/>
<ProgressBar android:id="@+id/p2" style="?android:attr/progressBarStyleHorizontal" android:progressDrawable="@drawable/greenprogress"  android:layout_width="wrap_content" android:layout_height="wrap_content"  android:layout_marginRight="5dp" />
<TextView android:id="@+id/tv3"  android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="0"   /> 

并使用此代码通过将此布局扩展到表格行来动态创建表格行

关于android - 在表布局中克隆行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18566746/

相关文章:

android - 我们可以在 aws 设备场中禁用用于 Android espresso 测试的对话框吗

android - 将用户数据从 Meteor 导出到 .CSV 文件

android - 如何用圆角制作 View ?

android - 以编程方式设置 ImageView 的对齐方式

java - 使用 Kotlin 扩展 java 类首选项时的多个构造函数

android - 错误 : A factory has already been set on this LayoutInflater (change background color of menu options)

android - RecyclerView - Layout_weight 在第一次加载时被忽略

Android 跨页面传播 TableLayout

android - 在 TableLayout 中设置 TableRow 的列

java - Android Studio无法建立我的APK?