How to create column-like layout for ListView rows

2019-07-04 02:51发布

I have a relative layout which looks like this: alt text

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?

3条回答
你好瞎i
2楼-- · 2019-07-04 03:16

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>
查看更多
虎瘦雄心在
3楼-- · 2019-07-04 03:18

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)

查看更多
我想做一个坏孩纸
4楼-- · 2019-07-04 03:23

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.

查看更多
登录 后发表回答