【问题标题】:TextView alignment in the TableRowTableRow 中的 TextView 对齐方式
【发布时间】:2013-12-12 18:53:24
【问题描述】:

TableLayout中有两个TableRow,每个TableRow包含两个TextView:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" >

        <TextView
            android:id="@+id/textTime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp" />

    </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="63dp"
            android:layout_marginRight="10dp" >

            <TextView
                android:id="@+id/textHall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/textPrice"
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp" />

    </TableRow>

</TableLayout>

如果textHall 包含一个短的WordtextTitle (Some Text) 位于正确的位置:

但是如果textHall 包含一个长的Second Word,那么在textTitle (Some Text) 之前会出现一个很大的缩进:

是什么原因?

谢谢。

【问题讨论】:

    标签: android android-xml android-tablelayout


    【解决方案1】:

    您应该为布局和行赋予权重。我修改了你的代码。这是应该的。

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:weightSum="2" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:orientation="horizontal" >
    
            <TextView
                android:id="@+id/textTime"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="10:30" />
    
            <TextView
                android:id="@+id/textTitle"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_gravity="left"
                android:layout_marginLeft="10dp"
                android:text="Some Text" />
        </LinearLayout>
    </TableRow>
    
    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="63dp"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:weightSum="2" >
    
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:orientation="horizontal" >
    
            <TextView
                android:id="@+id/textHall"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Some Word" />
    
            <TextView
                android:id="@+id/textPrice"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_marginLeft="10dp"
                android:layout_weight="1"
                android:text="200" />
        </LinearLayout>
    </TableRow>
    

    您可以根据需要为第二行设置任何边距值。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      您的 textHalltextTime 都属于同一列,并且该列的宽度设置为 textHall 的宽度(因为它具有更大的宽度),因此您的 textTime textview 的宽度在您增加时会增加增加textHall中的文字。

      【讨论】:

        猜你喜欢
        • 2011-09-16
        • 1970-01-01
        • 2021-10-27
        • 2013-04-19
        • 1970-01-01
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 2014-10-01
        相关资源
        最近更新 更多