Android 如何在tablelayout内对齐视图

Android 如何在tablelayout内对齐视图,android,textview,tablelayout,android-tablelayout,Android,Textview,Tablelayout,Android Tablelayout,在下面的xml布局中,我想创建一个承载一些文本视图的表布局。下面发布的xml布局的结果是四个文本视图,就在每个文本视图的旁边 其他的没有间隔。我知道我可以使用边距使每个视图与另一个视图保持一点距离,但我想使用重力方向来实现视图之间的间距 布局重力 我想要的结果是 odometer:value space space space space space tfu:val 我希望无论里程表数值的位数是多少,tfu始终显示在同一位置。换言之,考虑以下内容 我不想要的例子 odometer:123456

在下面的xml布局中,我想创建一个承载一些文本视图的表布局。下面发布的xml布局的结果是四个文本视图,就在每个文本视图的旁边 其他的没有间隔。我知道我可以使用边距使每个视图与另一个视图保持一点距离,但我想使用重力方向来实现视图之间的间距 布局重力

我想要的结果是

odometer:value space space space space space tfu:val
我希望无论里程表数值的位数是多少,tfu始终显示在同一位置。换言之,考虑以下内容 我不想要的例子

odometer:1234567                            tfu:1234567
odometer:1234567890                            tfu:1234567
我想要的是,无论里程表的位数是多少,tfu textview必须始终显示在sam位置,如以下示例所示:

odometer:1234567                            tfu:1234567
odometer:1234567890121223434                tfu:1234567

please tell me how achieve that.
代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="*"
android:collapseColumns="*"
android:orientation="vertical">

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv_label_odometer"
        android:text="odometer: "/>
    <TextView
        android:id="@+id/tv_odometer"
        android:text="val"/>
    <TextView
        android:id="@+id/tv_label_tfu"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="tfu: "/>
    <TextView
        android:id="@+id/tv_tfu"/>
</TableRow>
</TableLayout>


</RelativeLayout>

试试这个

<RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                    >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"/>

                    </RelativeLayout>

这就是我用的那个。一个在右边,一个在左边。如果需要更多,只需重复此操作,并使用边距调整空间。

尝试此操作

<RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                    >

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"/>

                    </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="*"
    android:collapseColumns="*"
    android:orientation="vertical">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        <TextView
            android:id="@+id/tv_label_odometer"
            android:text="odometer: "/>
        <TextView
            android:id="@+id/tv_odometer"
            android:text="val"/>
    </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        <TextView
            android:id="@+id/tv_label_tfu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="tfu: "/>
        <TextView
            android:id="@+id/tv_tfu"/>
        </LinearLayout>
    </TableRow>
</TableLayout>
</RelativeLayout>

这就是我用的那个。一个在右边,一个在左边。如果需要更多,只需重复,并使用边距调整间距。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<TableLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:shrinkColumns="*"
    android:collapseColumns="*"
    android:orientation="vertical">

    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        <TextView
            android:id="@+id/tv_label_odometer"
            android:text="odometer: "/>
        <TextView
            android:id="@+id/tv_odometer"
            android:text="val"/>
    </LinearLayout>
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">
        <TextView
            android:id="@+id/tv_label_tfu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="tfu: "/>
        <TextView
            android:id="@+id/tv_tfu"/>
        </LinearLayout>
    </TableRow>
</TableLayout>
</RelativeLayout>
添加一个LinearLayout以包装两个TextView,并将其宽度设置为0dp,布局权重设置为1。然后两个LinearLayout将具有其父级宽度的一半,我认为这可以解决您的问题。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_label_odometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="odometer: " />

            <TextView
                android:id="@+id/tv_odometer"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="1234567" />

            <TextView
                android:id="@+id/tv_label_tfu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="tfu: " />

            <TextView
                android:id="@+id/tv_tfu"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1234567" />
        </TableRow>
    </TableLayout>

</RelativeLayout>
添加一个LinearLayout以包装两个TextView,并将其宽度设置为0dp,布局权重设置为1。然后两个LinearLayout将具有其父级宽度的一半,我认为这可以解决您的问题。


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/tv_label_odometer"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="odometer: " />

            <TextView
                android:id="@+id/tv_odometer"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:text="1234567" />

            <TextView
                android:id="@+id/tv_label_tfu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="tfu: " />

            <TextView
                android:id="@+id/tv_tfu"
                android:layout_width="0sp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1234567" />
        </TableRow>
    </TableLayout>

</RelativeLayout>


这是真的,但我想你忘了在第一个线性布局中为textview添加宽度和高度是的,我只是复制你的代码并添加
LinearLayout
以包装
textview
,你可以根据需要添加这些。这是真的,但我想你忘了在第一个线性布局中为textview添加宽度和高度是的,我只需复制您的代码并添加
LinearLayout
以包装
TextView
,您可以根据需要添加这些内容。