2011-01-06

Border in a TableLayout

As specified in the documentation for TableLayout, borders are not supported (there is no attribute android:border or something like that). So you have to be a little creative to accomplish this.

In the root layout, specify a background color (this will be the border color), for instance:
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black">
Then for each cell in the table specify another background color, and set a margin:
<TextView
    android:id="@+id/tv_col2"
    android:textColor="@android:color/black"
    android:gravity="center_horizontal"
    android:layout_margin="1dp"
    android:background="@android:color/white"
    android:text="Col2" />
This will produce the following layout, depending on the number of rows and columns of course:

2 kommentarer: