如何在Android中使用表格行垂直对齐TextView字段

4
2个回答

14

你可以在TableRow上设置android:gravity="center_vertical"属性,使其子项垂直居中对齐。

此外,您还可以使用android:layout_marginLeft属性或android:layout_marginRight属性从左右两侧添加一些边距;同样地,您可以使用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

您可以在XML文件页面中使用以下标签来对齐内容,如果您能提供您所面临的问题的详细信息,我可以给出更多的解释。

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

第一段代码从左侧给出了25dp的边距,第二段代码只是将字段对齐到imageview1下方的右侧。

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

希望这可以帮助您。谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接