ListView Android中的列

Tor*_* Bø 6 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: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>
Run Code Online (Sandbox Code Playgroud)

问题是f.ex. wind_direction从"4"变为"300",然后列不对齐.

问题http://img185.imageshack.us/img185/2812/deviceo.png

谁可以用固定宽度的列制作并使用与设备无关的整个宽度?

Chr*_*ght 7

您可以在项目布局xml中将layout_weight与layout_width一起使用,如下所示:

<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>
Run Code Online (Sandbox Code Playgroud)

基本上,0dp的layout_width强制所有文本视图没有宽度.由于在确定文本视图的宽度之后根据layout_weight抛出线性布局中的备用宽度,所以在具有相同宽度(0)的文本框中以均匀的游戏场开始.没有0宽度,文本框以不同的宽度开始,并且添加额外的空间(通过layout_weight)仍然使文本框的宽度不同.

  • 这仅在列的宽度相互关联时才有效. (2认同)

Che*_*mon 3

考虑使用 TableLayout 而不是 ListView。它被设计为具有行和列。请参阅Hello TableLayout

要以编程方式添加行,您可以膨胀 TableRow 并在 TableLayout 上调用 addView。