Columns in a TableRow divided using weights & weightsums not spacing elements properly
Question
I've been assigned to create an activity that displays table of changes made to the app by version. The table has four columns, Id, Type, Description, and Version.
To do this, I placed a TableLayout in a scrollView. Then, I have a template for the TableRows, that are inflated dynamically at Runtime. The column widths should stay static; they can overflow in height but not width.
To achieve this, I tried using the weight & weight: sum property of the column and row container respectively. I have set the layout_width
to 0
on both.
The problem is that the columns are expanding outside the screen boundaries. I would post the images but I don't have the authority.
(Landscape looks close but you can see that the outer boundry is still being cut off).
I noticed that in if the weights of the children add to about 45% of the total then the weighting comes close to looking right.
Finally, the views separating the columns have been given a weight of zero, and 1dip width. (I'm not sure if that could be causing a problem).
To me it looks like that weighting is using the width as if it was in landscape mode. Do I have to make two layouts, one for portrait, and one for landscape?
I've added the XML layout for the table row:
<?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="match_parent" >
<LinearLayout
android:id="@+id/table_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/top_border"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" />
<LinearLayout
android:id="@+id/horizontal_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<View
android:id="@+id/view1"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_id"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Id" android:textSize="10sp" android:layout_weight="0.5"/>
<View
android:id="@+id/view2"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_type"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Type" android:textSize="10sp" android:layout_weight="1"/>
<View
android:id="@+id/view3"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_desc"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Description" android:textSize="10sp" android:layout_weight="2"/>
<View
android:id="@+id/view4"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_version"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Version" android:textSize="10sp" android:layout_weight="0.5"/>
<View
android:id="@+id/view6"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
</LinearLayout>
<View
android:id="@+id/view7"
android:layout_width="match_parent"
android:layout_height="1dip" android:background="#FFFFFF"/>
</LinearLayout>
</TableRow>
The upper and lower view are top and bottom borders.
And here is the layout for the table that its getting added too:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" android:scrollbarAlwaysDrawVerticalTrack="true">
<ScrollView
android:id="@+id/view_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:fillViewport="true">
<TableLayout
android:id="@+id/table_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:shrinkColumns="0"
android:stretchColumns="0" >
</TableLayout>
</ScrollView>
</LinearLayout>
Solution
Thanks MH.
Sorry for not getting back to you sooner. (I'm new to Android and Stack Overflow...and I didn't realize that you answered my questsion.) I've added the row layout.
<?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="match_parent" >
<LinearLayout
android:id="@+id/table_row_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:id="@+id/top_border"
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#FFFFFF" />
<LinearLayout
android:id="@+id/horizontal_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="4" >
<View
android:id="@+id/view1"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_id"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Id" android:textSize="10sp" android:layout_weight="0.20"/>
<View
android:id="@+id/view2"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_type"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Type" android:textSize="10sp" android:layout_weight="0.5"/>
<View
android:id="@+id/view3"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_desc"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Description" android:textSize="10sp" android:layout_weight="1"/>
<View
android:id="@+id/view4"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
<TextView
android:id="@+id/txt_version"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="Version" android:textSize="10sp" android:layout_weight="0.25"/>
<View
android:id="@+id/view6"
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#FFFFFF" android:layout_weight="0.01"/>
</LinearLayout>
<View
android:id="@+id/view7"
android:layout_width="match_parent"
android:layout_height="1dip" android:background="#FFFFFF"/>
</LinearLayout>
</TableRow>
David