Android 列表视图中的列
如何在Android ListView中创建列?我有以下列表项布局xml:Android 列表视图中的列,android,listview,Android,Listview,如何在Android ListView中创建列?我有以下列表项布局xml: <?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="fill_parent" android:orie
<?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="fill_parent"
android:orientation="horizontal">
<TextView android:layout_weight=".3" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/hour"
android:gravity="center" />
<ImageView android:id="@+id/symbol" android:scaleType="fitCenter"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight=".5" />
<TextView android:layout_weight=".8" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/temperature"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/percipitation"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/wind_speed"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/wind_direction"
android:gravity="center" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
问题是f.ex。风向从“4”变为“300”,则立柱未对齐
使用固定宽度的列并使用与设备无关的整个宽度,谁能做到这一点?考虑使用TableLayout而不是ListView。这被设计为具有行和列。看
要以编程方式添加行,您可以膨胀TableRow并在TableLayout上调用addView。将两者合并。尝试对列表项包装使用TableLayout而不是LinearLayout。我想我曾经在某个地方看过那部作品。我自己从没试过
您可以执行以下操作之一,而不是在文本视图上包装内容:
android:layout_width="40dip"
上述设置与密度无关的宽度,或
android:layout_weight="2"
可用于设置每个文本视图的宽度百分比
另请参阅此帖子和链接:
您可以在项目布局xml中结合使用布局权重和布局宽度0,如下所示:
<?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="fill_parent"
android:orientation="horizontal">
<TextView android:layout_weight=".3" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/hour"
android:gravity="center" />
<ImageView android:id="@+id/symbol" android:scaleType="fitCenter"
android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight=".5" />
<TextView android:layout_weight=".8" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/temperature"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/percipitation"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/wind_speed"
android:gravity="center" />
<TextView android:layout_weight="1" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:id="@+id/wind_direction"
android:gravity="center" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<TextView
android:id="@+id/text2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"/>
</LinearLayout>
实际上,0dp的布局宽度强制所有文本视图没有宽度。由于线性布局中的剩余宽度是在确定文本视图的宽度后根据布局权重分配的,因此,在所有具有相同宽度(0)的文本框之间,您首先要有一个公平的竞争环境。如果没有0宽度,文本框开始时的宽度不同,添加额外的空间(通过布局权重)仍然会使文本框的宽度不同。如果要显示大量数据,请不要使用TableLayout。性能和内存消耗是可怕的。您的数据可能需要几秒钟才能显示出来,您将收到Choreographer错误,即您的应用程序在UI线程中进行了太多处理