gpt4 book ai didi

android - 不同行有不同列的 TableLayout

转载 作者:太空狗 更新时间:2023-10-29 14:31:59 25 4
gpt4 key购买 nike

我尝试创建一个包含多行的 TableLayout,并且每一行都有不同的列

下面显示 ListView 。对于 ListView 的每一行,它包含一个只有 1 行 2 列的表格布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/color_label"
android:layout_width="12dip"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:background="#ffffff" />

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:id="@+id/toptext"
android:gravity="left"
/>
<TextView
android:id="@+id/bottomtext"
android:gravity="right"
/>
</TableRow>

<View android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</LinearLayout>

这是结果。

enter image description here

后来想进一步完善TableLayout。它的第一行有 2 列。它的第二行有 4 列,垂直线将 1,2 列与 3,4 列分开。

我使用以下代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/color_label"
android:layout_width="12dip"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:background="#ffffff" />

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:id="@+id/toptext"
android:gravity="left"
/>
<TextView
android:id="@+id/bottomtext"
android:gravity="right"
/>
</TableRow>

<View android:layout_height="2dip"
android:background="#FF909090" />

<TableRow>
<TextView
android:id="@+id/col1"
android:gravity="left"
android:text="Column 1"
/>
<TextView
android:id="@+id/col2"
android:gravity="right"
android:text="Column 2"
/>
<TextView
android:id="@+id/col3"
android:gravity="left"
android:text="Column 3"
/>
<TextView
android:id="@+id/col4"
android:gravity="right"
android:text="Column 4"
/>
</TableRow>


<View android:layout_height="2dip"
android:background="#FF909090" />

</TableLayout>
</LinearLayout>

这是结果。

enter image description here

但是,表格布局的第二行不是我想要的。我想要的是这样的东西。

enter image description here

如何改进我的 XML 以达到上述效果?

最佳答案

您可以为不同的项目渲染器创建自己的可绘制 xml(在 res/drawable-[h|m|l]dpi 内),并将它们设置为背景。

通过这种方式,您可以实现任何类型的边框、形状等,并且在您的适配器getView方法中,您只需使用适当的方法膨胀您需要的 View backgroundDrawable.

关于android - 不同行有不同列的 TableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5794499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com