Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何强制左对齐的项目包装而不是右对齐的项目?_Android_Android Layout - Fatal编程技术网

Android:如何强制左对齐的项目包装而不是右对齐的项目?

Android:如何强制左对齐的项目包装而不是右对齐的项目?,android,android-layout,Android,Android Layout,这是我的布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:minHeight="?android:

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:minHeight="?android:attr/listPreferredItemHeight">
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        <TextView
            android:id="@+id/customer_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:paddingLeft="4dip"/>
        <TextView
            android:id="@+id/ticket_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:paddingLeft="6dip"/>
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="right">
        <TextView
            android:id="@+id/total"
            android:gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:paddingRight="6dip"/>
        <TextView
            android:id="@+id/date"
            android:gravity="right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="14sp"
            android:paddingRight="6dip"/>
    </LinearLayout>
</LinearLayout>

这将生成如下所示的列表项:


正如你所看到的,它有点难看。如果右上角的框是红色的,我希望顶部的蓝色框可以换行,并且底部的一行完全独立于顶部的对齐方式,这样无论顶部的一行是什么样子,日期都不会换行。实现这一点的最佳方法是什么?

使用
表格布局,使其具有
shrinkColumns=“0”
,并将客户名称/票据指定在第0列中。您还可以为要展开以适应的列设置strechColums,因此可能需要第1列来展开日期列

另一种选择是不将它们放在垂直线性布局中,而是为每行制作水平布局。

实现:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:shrinkColumns="0"
        android:stretchColumns="1"
        android:minHeight="?android:attr/listPreferredItemHeight">
    <TableRow>
        <TextView
            android:id="@+id/customer_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_span="2"
            android:textSize="18sp"
            android:paddingLeft="4dip"/>
        <TextView
            android:id="@+id/total"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:gravity="right"
            android:layout_gravity="right"
            android:textSize="18sp"
            android:paddingRight="6dip"/>
    </TableRow>
    <TableRow>
        <TextView
            android:id="@+id/ticket_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:textSize="14sp"
            android:paddingLeft="6dip"/>
        <TextView
            android:id="@+id/date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_span="2"
            android:layout_gravity="right"
            android:gravity="right"
            android:textSize="14sp"
            android:paddingRight="6dip"/>
    </TableRow>
</TableLayout>


谢谢。我已经在对这个问题的单独回答中发布了一个实现,但接受了您的正确回答。