Table with dynamic number of columns in Android

筅森魡賤 提交于 2019-12-24 07:26:00

问题


I want to create a table with dynamic number of columns in android I know how to add rows dynamically using LayoutInflator but how to add columns dynamically?

Thank you for your time any help is highly appreciated.


回答1:


you dont add columns - only rows and after that you can tell every view how many columns need to span

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

one of the best Tuts: http://mobile.tutsplus.com/tutorials/android/android-sdk_table-layout/




回答2:


Use Listview and create a custom layout for row.Use a linear layout with orientation horizontal for row

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

inside this linear layout you can add any number of columns.You can control the width by setting layout weight,You can do this in java dynamically.And make the width of each cell zero and height match_parent.So that your text will be correctly wrapped in each cell.

Setting weight is better than setting width directly since it uses free space and your table will be correctly displayed inside the screen

//Edit

Add a linearlayout in your layout

<LinearLayout 

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

Next we have to create columns.Actually it should be like this, here you get 2 columns with half size

<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"
     />

If you want to do this dynamically you have to write code to create Textview or any other view in java src and set its layout weight as 100/no of columns. You can set weight in layout parameters.



来源:https://stackoverflow.com/questions/21089066/table-with-dynamic-number-of-columns-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!