开发者

Inserting vertical rows in TableLayout

I'm trying to add rows to a TableLayout in Android and the orientation property doesn't seem to work. Basically I need to create a TableRow, add multiple TextViews to it, and the add to the TableLayout and have the TextViews stacked vertically instead of horizontally.

The XML looks like this:

<TableLayout开发者_如何学C android:id="@+id/mylayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TableRow  android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            <TextView android:text="Test1"></TextView>
            <TextView android:text="Test2"></TextView>
    </TableRow>
</TableLayout>

This seems like it should work, but the TextViews end up stacked left to right. Any thoughts?


We can do by inserting TableLayout inside TableRow,like this.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TableLayout android:id="@+id/mylayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
        <TableRow  android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TableLayout android:id="@+id/mylayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical">
            <TextView android:text="Test1"></TextView>
            <TextView android:text="Test2"></TextView>
            </TableLayout>
        </TableRow>
    </TableLayout>
  </LinearLayout>


Try changing

        <TextView android:text="Test1"></TextView>
        <TextView android:text="Test2"></TextView>

to:

        <TextView android:text="Test1" android:layout_column="0"></TextView>
        <TextView android:text="Test2" android:layout_column="0"></TextView>

See the documentation TableLayout ( http://developer.android.com/reference/android/widget/TableLayout.html ) it says

Cells must be added to a row in increasing column order, both in code and XML. Column numbers are zero-based. If you don't specify a column number for a child cell, it will autoincrement to the next available column. If you skip a column number, it will be considered an empty cell in that row. See the TableLayout examples in ApiDemos for examples of creating tables in XML.


Create a LinearLayout with vertical orientation and have your text views inside it. Then add this LinearLayout to the TableRow

<TableLayout android:id="@+id/mylayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TableRow  android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
             <LinearLayout
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:orientation="vertical" >
                     <TextView android:text="Test1"></TextView>
                     <TextView android:text="Test2"></TextView>
             </LinearLayout>
    </TableRow>
</TableLayout>


I found a solution to this problem.I made a RelativeLayout and I am programmatically adding vertical orientation LinearLayout to it for each vertical row.I keep a handle to the control ID of the last inserted LinearLayout and insert the new one to the right of that ID. Feels a bit kludgy, but it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜