如何为ListView行创建类似列的布局?

3
我有一个布局,它的样子像这样: alt text 以下是代码:
<?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数字对齐呢?
3个回答

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>

像往常一样,非常稳定的回答!由于它在ListView中,使用TableLayout而不是RelativeLayout会影响性能吗? - Sheehan Alam
嗯,恐怕我在这方面不能提供太多帮助。我不确定那会如何影响性能。我认为它不会造成太大的差异。 - Kevin Coppock

1

不太清楚你想要实现什么效果,但是这里有几个选项:

如果你希望列在行之间对齐,可以考虑使用TableLayout

你还可以使用"weight"属性来影响每个TextView所占屏幕的百分比。如果你在最左边的文本视图上分配一个值为1,在其他两个文本视图上分配值为0,这将导致左侧的文本视图占据所有额外的空间,从而将中间的文本视图推向右侧。不过这可能会导致中间的文本视图看起来右对齐。你也可以通过分别分配2、5和3来给左侧、中间和右侧的文本视图分别分配20%、50%和30%的宽度。

你也可以为文本视图设置一个明确的大小(以dpi为单位),但这可能在不同尺寸的屏幕上出现问题。


是的,我希望公司名称与价格右对齐。 - Sheehan Alam
你能否用代码展示如何进行表格布局?我插入了一行表格,但现在TextViews垂直堆叠而不是水平排列。 - Sheehan Alam
查看hello tableLayout:http://developer.android.com/guide/tutorials/views/hello-tablelayout.html。几乎每种布局或视图类型都有很好的教程。 - Cheryl Simon

0

我同意Mayra的观点!使用TableView,您将获得所需的格式化效果,并保留ListView的几乎所有功能(滚动和列表导向功能)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接