开发者

How to align table content in Android

开发者 https://www.devze.com 2023-04-03 06:49 出处:网络
I want to align content of TableLayout in android. I want align the table headers too. what changes needed in main.xml?

I want to align content of TableLayout in android. I want align the table headers too. what changes needed in main.xml?

<TableRow android:layout_margin="0dp" android:id="@+id/myRow"> 
<TextView android:id="@+id/myTitle" android:layout_span开发者_开发知识库="5" android:text="" android:layout_gravity="center_horizontal" />
</TableRow> 
<TableRow> <TextView android:layout_span="5" android:background="#000000" android:height="1dp" android:gravity="right" /> 
 </TableRow>

How to align table content in Android


See the Developer pages about Common Layout Objects. You have to add gravity to your elements inside your table.


No problem since every table row has its own definition.

use the "gravity" to achieve your needs, for example

<TableRow android:id="@+id/tableRow1" android:layout_height="wrap_content" android:layout_width="fill_parent">
    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="left"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical|center_horizontal"></TextView>
</TableRow>
  • the textbox in the first column is left-alligned
  • the textbox in the second column is centered vertically and horizontally

which turns out to look like this:

How to align table content in Android

have a look here for gravity details

additionally you can use padding to preserve some space left/right/above/under

0

精彩评论

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