繁体   English   中英

Android中具有动态列数的表

[英]Table with dynamic number of columns in Android

我想在Android中创建具有动态列数的表,我知道如何使用LayoutInflator动态添加行,但是如何动态添加列?

感谢您的宝贵时间,我们将为您提供任何帮助。

您不添加列-仅添加行,然后您可以告诉每个视图需要跨越多少列

android:layout_span 

Defines how many columns this child should span. Must be >= 1. 

Must be an integer value, such as "100". 

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type. 

This corresponds to the global attribute resource symbol layout_span.

http://developer.android.com/reference/android/widget/TableRow.LayoutParams.html#attr_android:layout_span

最好的Tuts之一: http//mobile.tutsplus.com/tutorials/android/android-sdk_table-layout/

使用Listview并为行创建自定义布局,将水平方向的线性布局用于行

 android:orientation="horizontal"
 android:weightSum="100" // or some other values

在此线性布局中,您可以添加任意数量的列。您可以通过设置布局权重来控制宽度,可以在Java中动态地执行此操作。并使每个单元格的宽度为零,高度为match_parent,这样您的文本即可正确换行在每个单元格中。

设置权重比直接设置宽度要好,因为它会占用可用空间,并且您的表格将正确显示在屏幕内部

//编辑

在布局中添加线性布局

<LinearLayout 

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

接下来我们必须创建列,实际上应该是这样,在这里您将获得2个具有一半大小的列

<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
     />
<TextView
        android:id="@+id/textview"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="50"
     />

如果要动态执行此操作,则必须编写代码以在Java src中创建Textview或任何其他视图,并将其布局权重设置为100 /无列。 您可以在布局参数中设置权重。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM