Android的行/列布局设计与完美的对齐

gkl3eglg  于 2023-05-21  发布在  Android
关注(0)|答案(6)|浏览(143)

我尝试设计布局与线性布局垂直和水平设计多行与两列,并试图完美的右列对齐,但列是左列的字数左右移动...我怎么能解决这个问题,帮助我…

我的XML是……

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#b4cde6"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="Order Id"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="564789"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="10dp"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="left"
                android:text="Order Datednhgv"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="19/04/2014"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
zmeyuzjn

zmeyuzjn1#

试试这个。
将所有LinearLayout宽度android:layout_width="wrap_content"更改为android:layout_width="0dp",如下所示。

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >
axr492tv

axr492tv2#

请尝试使用此布局代码。

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#b4cde6"
    android:orientation="horizontal"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Order ID"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:paddingLeft="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="564789"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Order Datednhgv"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:paddingLeft="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="19/04/2014"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>
</LinearLayout>
7tofc5zh

7tofc5zh3#

您应该使用TableLayout进行此类设计。你会在这里得到更多的信息。http://developer.android.com/reference/android/widget/TableLayout.html

toe95027

toe950274#

尝试使用此代码实现对齐布局。如果您使用此布局作为列表视图的自定义布局,请删除第二个线性布局并与列表视图一起使用。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#b4cde6"
    android:orientation="vertical"
    android:padding="10dp" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:text="Order ID"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:gravity="right"
            android:text="564789"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:text="Order Datednhgv"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:gravity="right"
            android:text="19/04/2014"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

</LinearLayout>
44u64gxh

44u64gxh5#

试试这个

<LinearLayout android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:background="#aee4e4"
            android:weightSum="2"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_weight="1"
                android:text="Order Id"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:textSize="18sp"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_weight="1"
                android:text="564789"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:textSize="18sp"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <LinearLayout
            android:weightSum="3"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_weight="1"
                android:text="Order Datednhgv"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:textSize="18sp"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_weight="2"
                android:text="19/04/2014"
                android:layout_margin="10dp"
                android:layout_width="wrap_content"
                android:textSize="18sp"
                android:layout_height="wrap_content" />

        </LinearLayout>
    </LinearLayout>
daolsyd0

daolsyd06#

如果你想创建多行,最好选择列表视图。与添加多个布局相比,列表视图是更好的选择。Here是处理listview的简单示例。

相关问题