【发布时间】:2021-04-30 10:31:05
【问题描述】:
我正在尝试在线性布局中添加一个表格。在我的线性布局中有textview、spinner。当我尝试添加表格布局时,它没有正确对齐。下面是我的代码
<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>
输出
预期输出
我一定错过了一些我不知道的东西。我怎样才能像上图一样将它们对齐
任何帮助将不胜感激。
【问题讨论】:
-
如果你是 android 开发新手,我建议不要在 LinearLayout 上浪费时间,只需学习约束布局
-
这样嵌套的Linear Layout会很慢,用约束布局和recyclerview代替Table Layout会好很多
标签: android android-layout android-tablelayout