簡體   English   中英

如何為ListView行創建類似列的布局?

[英]How to create column-like layout for ListView rows?

我有一個相對布局,看起來像這樣: 替代文字

這是代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >   
    <TextView
        android:id="@+id/nameText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_alignParentLeft="true"
        android:text="Symbol"
        />
    <TextView
        android:id="@+id/priceText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_toRightOf="@+id/nameText"
        android:gravity="right"     
        android:text="100"
     />  
    <TextView
        android:id="@+id/changeText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:layout_toRightOf="@+id/priceText"
        android:gravity="right"     
        android:text="1.3"
        />  
</RelativeLayout>

如何讓公司名稱排在1.3號旁邊?

跟隨Mayra的回答,這是使用layout_weight實現它的一種方法:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    >
    <TableRow>  
        <TextView
            android:id="@+id/left_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="left|center_vertical"
            android:padding="5dip"
            android:layout_weight="0"
            android:text="Code"
            />
        <TextView
            android:id="@+id/middle_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="right|center_vertical"
            android:padding="5dip"
            android:layout_weight="1"
            android:text="Name of Company"
            />      
        <TextView
            android:id="@+id/right_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:gravity="right|center_vertical"
            android:layout_weight="0"       
            android:text="1.3"
            />
    </TableRow>
</TableLayout>

目前尚不清楚你想要發生什么,但這里有幾個選擇:

如果希望列跨行對齊,可以考慮使用TableLayout

您還可以使用“權重”屬性來影響每個TextView占用屏幕的百分比。 如果在最左側的文本視圖中指定值1,而在另一個文本視圖上指定0,則這將導致左側文本視圖占用所有額外空間,從而將中間文本視圖推向右側。 這可能會導致中間文本視圖看起來正確對齊。 你可以給左邊一個20%,中間一個50%,右邊一個30%,分配2,5和3。

您也可以為文本視圖設置一個明確的大小(以dpi為單位),但這可能是使用不同大小的屏幕的問題。

我同意Mayra! 使用TableView,您將獲得所需的格式以及幾乎所有ListView功能(scolling和面向列表的功能)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM