繁体   English   中英

左右对齐文本视图

[英]Align textviews to left and right

我对textviews对齐有疑问。 我想将两个textview的listview项目对齐到左侧和右侧。 在Android Studio编辑器中可以,但是当我将应用程序部署到设备上时,它并没有正确对齐。 我不知道是什么问题。 下面是我的布局:

找到了问题。 问题实际上是由于我错误地设置了Listview的layout_width =“ wrap_content”,但应该是match_parent。 正如提到的布局是列表的项目布局。 感谢大家。

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="@dimen/activity_horizontal_margin"
              android:orientation="vertical">

    <TextView
        android:id="@+id/tv_order_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/tv_order_category_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"/>

    <TextView
        android:id="@+id/tv_order_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="35sp"/>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_tin_ein"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_tin_ein"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="1"
            android:text="Test"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="@string/order_address"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_address"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_email"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_email"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_phone"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_phone"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_mobile"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_mobile"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_fax"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_fax"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_created_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_created_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_start_working_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_start_working_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_end_date"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_end_date"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_cost"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_cost"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/order_vat"
            android:gravity="left"
            android:layout_weight="1"
            android:textStyle="bold"/>

        <TextView
            android:id="@+id/tv_order_vat"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"/>
    </TableRow>

</TableLayout>   

我认为您的代码很好,而这一代码将更好地改善您的代码。 如果您想将重力放在右边,只需使用重力:右边。

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="asd"
            android:gravity="left"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tv_order_tin_ein"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="right"
            android:text="Test" />
    </LinearLayout>

尝试这个 :

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weightSum="2">
            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Text1"
                android:layout_weight="1"/>
            <TextView
                android:text="Text2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
        </LinearLayout>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM