Right align column in android table layout

2019-04-03 16:09发布

问题:

I want to draw a table in which last column should be at the right most side of the table.

This is how the table row looks like:

Admin (2)New
Network (2)New

And this is how it should be:

Admin (2)         New
Network (2)       New

XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="45dp"
    android:gravity="center" android:background="@color/list_bg">

    <TableLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TableRow>



            <ImageView android:id="@+id/t1" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/t2" android:typeface="normal"
                android:singleLine="true" android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t10" android:typeface="normal"
                android:singleLine="true" android:text=" " android:textSize="14sp"
                android:textStyle="normal" android:layout_width="wrap_content"
                android:textColor="#000000" android:layout_height="wrap_content" />
            <TextView android:id="@+id/t4" android:typeface="normal"
                android:visibility="gone" android:singleLine="true" android:text="("
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t5" android:typeface="normal"
                android:visibility="gone" android:singleLine="true"
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t6" android:typeface="normal"
                android:visibility="gone" android:singleLine="true" android:text=")"
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <ImageView android:id="@+id/t3" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t7" android:typeface="normal"
                android:visibility="visible" android:singleLine="true" android:text="New"
                android:textSize="14sp"
                android:textStyle="normal" android:layout_width="wrap_content"
                android:textColor="#000000" android:layout_height="wrap_content" />

        </TableRow>

    </TableLayout>

</RelativeLayout>

In this xml t7 should be right at right most side of the table, how to do this???

回答1:

Here you have to do

  1. set the table row width to fill_parent

  2. and set the android:layout_gravity="right" to the textview which you want to align right it to the table row

    ---- or ----

  1. add the weight to the textview inside a table row so that they can align how you want.


回答2:

  1. android:stretchColumns="x" where x is the column you want to fill
  2. android:layout_width="fill_parent" to ensure everything fills the screen
  3. android:layout_gravity="right" for the view component in column "x"

example (pseudo code):

<TableLayout
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1">

    <TableRow>
        <TextView android:text="Left Column" />
        <TextView android:layout_gravity="right|center_vertical" android:text="Right Adjusted Column" />
    </TableRow>

</TableLayout>