I'm trying to implement a 40% – 20% – 20% – 20% split (or something close to that) with four TextViews inside a TableLayout.
Problem is, the leftmost column takes too much space. In other words, the 3 columns on the right do not take the space they need. The longer text on the left should be split on multiple lines if needed, never the 3 shorter texts on the right.
The text marked with red should not be split on two lines.
I even tried android:maxLines="1"
on the right-hand columns, and android:lines="2"
on the leftmost column, and still it won't work as intented (→ there's just one line alright, but rightmost columns get clipped).
I also tried this suggestion and set android:layout_width="0dp"
on the leftmost column.
Well, that gives me the opposite problem: that column takes too little space (long titles get clipped), and the 3 on the right take too much.
Opposite problem with android:layout_width="0dp"
I don't want to hardcode any DP width values for the columns, to best support different screens. To me, specifying relative widths with layout_weight
sounds exactly what I need, if only I got it working... Any ideas what I'm doing wrong?
Android 4.4.2. My test device is Nexus 7, on which I run into this problem in portrait mode.
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
Layout XML for one row (corresponding to the first screenshot above):
<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>
<TextView
android:id="@+id/title"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="center_vertical|left"
android:textSize="16dp"
android:textStyle="bold"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="@+id/rating"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center"
android:text="(todo)"
android:textSize="16dp"
android:layout_marginTop="8dp"
/>
<TextView
android:id="@+id/pages_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center"
android:textSize="16dp"
android:maxLines="1"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="@+id/date_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:gravity="center_vertical|right"
android:textSize="16dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
/>
</TableRow>
Surrounding TableLayout
and ScrollView
:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- table rows added dynamically in code -->
</TableLayout>
</ScrollView>