[英]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? 如何让公司名称排在1.3号旁边?
Following up on Mayra's answer, here's one way to do it using layout_weight: 跟随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>
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 . 如果希望列跨行对齐,可以考虑使用TableLayout 。
You can also use the "weight" atribute to affect what percentage of the screen each TextView takes up. 您还可以使用“权重”属性来影响每个TextView占用屏幕的百分比。 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. 如果在最左侧的文本视图中指定值1,而在另一个文本视图上指定0,则这将导致左侧文本视图占用所有额外空间,从而将中间文本视图推向右侧。 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. 你可以给左边一个20%,中间一个50%,右边一个30%,分配2,5和3。
You could also just set an explicit size for the text views (in dpi), but this might be problamatic with different sized screens. 您也可以为文本视图设置一个明确的大小(以dpi为单位),但这可能是使用不同大小的屏幕的问题。
I agree with Mayra! 我同意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) 使用TableView,您将获得所需的格式以及几乎所有ListView功能(scolling和面向列表的功能)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.