如何使用Android中的表行垂直对齐textview字段

时间:2012-11-13 06:15:00

标签: android row

我有这个输出

http://www.cubixshade.com/images/align_row.jpg

我希望第一个字段是日期字段垂直对齐中心? 我应该用什么来在其他领域左右添加一些空间呢?

2 个答案:

答案 0 :(得分:10)

您可以将android:gravity="center_vertical"属性设置为TableRow,使其子项垂直居中。

此外,您可以使用android:layout_marginLeft属性或android:layout_marginRiight属性从左侧和右侧放置一些边距,类似地,您可以使用android:layout_marginTopandroid:layout_marginBottom将边距置于顶部和底部。

如果您想拉伸色谱柱,则可以使用android:layout_weight属性来定义色谱柱的重量。

作为参考,您可以看到以下带有2列的示例

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1" >

    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Male"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etNoOfMale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:inputType="number"/>
    </TableRow>

    <TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Female"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/etNoOfFemale"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:inputType="number"/>
    </TableRow>

</TableLayout>

答案 1 :(得分:-1)

你可以在你的xml文件页面中使用下面的标签来正确对齐,如果可以提供你所面对的pblm的详细信息,我可以给出更多解释。

android:layout_marginLeft="25dp"
android:layout_below="@+id/imageView1"

第一个代码从左边开始给出25dp的边距,第二个代码只是将字段对齐到imageview1下面。

还有另一种方法 只需右键单击文本视图或您在设计页面中添加的任何内容,选择其他属性 - >布局参数 - >然后选择您需要的项目[它包含所有对齐属性。

希望这个能帮助你。谢谢