【问题标题】:Align textviews to left and right将文本视图左右对齐
【发布时间】:2015-11-25 08:28:24
【问题描述】:

我的文本视图对齐有问题。我想将列表视图项的两个文本视图左右对齐。在 Android Studio 编辑器中没问题,但是当我将应用程序部署到设备时,它没有正确对齐。我不知道是什么问题。下面是我的布局:

发现问题。问题实际上出在 listview 上,我错误地设置了 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>   

【问题讨论】:

  • 你的代码没问题。当我们部署在我的设备中时。
  • 在我的设备上很奇怪,android 6.0 不行
  • 在设备中显示文本的位置
  • 第一个文本视图附近
  • 你在片段中使用它们吗?

标签: android


【解决方案1】:

我认为你的代码很好,这个代码会更好地改进你的代码。 只是使用重力:如果你想把它放在右边,那就对。

<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>

【讨论】:

  • 我试过了,但第二个 textview 没有向右对齐,右侧没问题,但它靠近第一个 textview。
  • 还有更多的文本视图,我想像我提到的那样对齐。
  • 你能告诉我你想要的布局吗?而当你的TextView的layout_width为match_parent时,你必须使用gravity而不是layout_gravity
  • 对我来说你的代码很好,但对你来说我不知道​​为什么它没有工作。所以我建议另一种方法,你可以使用RelativeLayout来做这个
【解决方案2】:

试试这个:

<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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 2012-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多