Android 在线性布局中添加表布局

Android 在线性布局中添加表布局,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我试图在线性布局中添加一个表。在我的线性布局中有文本视图,微调器。当我尝试添加表格布局时,它没有正确对齐。下面是我的代码 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/cardLayout&q

我试图在线性布局中添加一个表。在我的线性布局中有
文本视图
微调器
。当我尝试添加表格布局时,它没有正确对齐。下面是我的代码

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/cardLayout"
            android:orientation="vertical">
            <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Product"
                android:gravity="center"
                android:textStyle="bold"
                android:textSize="18sp"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10sp"
                android:layout_margin="10sp"
                android:orientation="horizontal">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".5"
                    android:orientation="vertical">

                    <Spinner
                        android:id="@+id/product_spinner"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        />


                </LinearLayout>

                <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="100dp"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    >
                    <TableRow android:background="#0079D6" android:padding="5dp">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Product Name" />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="Sale" />
                    </TableRow>
                    <TableRow android:background="#DAE8FC" android:padding="5dp">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="" />
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:text="" />
                    </TableRow>
                </TableLayout>

            </LinearLayout>

        </LinearLayout>

输出

预期产出

我一定错过了一些我不知道的东西。我怎样才能像上图一样将它们平均对齐

非常感谢您的帮助。

请尝试使用以下代码:

    <LinearLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Product"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="18sp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center">
        <Spinner
            android:id="@+id/product_spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            />

        <TableLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:layout_gravity="center"
            >
            <TableRow android:background="#0079D6" android:padding="5dp">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Product Name" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Sale" />
            </TableRow>
            <TableRow android:background="#DAE8FC" android:padding="5dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="" />
            </TableRow>
        </TableLayout>

    </LinearLayout>
</LinearLayout>


如果您是android开发新手,我建议您不要在LinearLayout上浪费时间,只需学习约束布局。这种嵌套的线性布局将非常缓慢,最好使用约束布局和recyclerview来代替表布局