简体   繁体   中英

How to create column-like layout for ListView rows?

I have a relative layout which looks like this: 替代文字

Here is the code:

<?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>

How can I get the company name to line up next to the number 1.3?

Following up on Mayra's answer, here's one way to do it using 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>

It's not clear exactly what you want to have happen, but here are a few options:

If you want columns to be aligned across rows, you might consider using a TableLayout .

You can also use the "weight" atribute to affect what percentage of the screen each TextView takes up. If you assign a value of 1 on the left-most text view, and 0 on the other 2, this will cause the left text view to take up all the extra space, and thus push the middle text view to the right. This will probably cause the middle text view to look right aligned though. You could instead give the left one 20%, the middle one 50% and the right one 30% by assign 2, 5 and 3.

You could also just set an explicit size for the text views (in dpi), but this might be problamatic with different sized screens.

I agree with Mayra! Use the TableView, you'll get the formatting your looking for as well as keep almost all functionality of a ListView (scolling, and list oriented functionality)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM