2010-09-24 92 views
1

我在創建具有4列的表格佈局時存在問題,這些列橫向跨越。如何創建一個4列的TableLayout?

我希望我的行看起來像這樣:

AAPL | 200.00 | Image Divider | 1.53(+ 1.5%)

我想要200.00右對齊,以及1.53(+ 1.5%)。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:background="@drawable/stocks_gradient" android:orientation="horizontal"> 
    <TableLayout android:layout_width="wrap_content" 
     android:layout_toLeftOf="@+id/priceText" android:layout_height="wrap_content" 
     android:id="@+id/TableLayout01"> 
     <TableRow android:layout_width="wrap_content" 
      android:layout_below="@+id/nameText" android:id="@+id/TableRow01" 
      android:layout_height="wrap_content"></TableRow> 
     <TextView android:id="@+id/nameText" android:layout_height="wrap_content" 
      android:padding="5dip" android:layout_alignParentLeft="true" 
      android:text="Symbol" android:textStyle="bold" android:textSize="24sp" 
      android:layout_width="100dp" android:textColor="#4871A8" /> 
     <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" android:textStyle="bold" android:textSize="24sp" 
      android:textColor="#4871A8" /> 
     <ImageView android:layout_toLeftOf="@+id/changeText" 
      android:id="@+id/verticalDivider" android:background="@drawable/vertical_divider" 
      android:layout_width="wrap_content" android:layout_height="48dp"></ImageView> 
     <TextView android:id="@+id/changeText" android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/priceText" android:gravity="right" 
      android:textSize="18sp" android:text="3.07(+1.08%)" android:padding="12dip" 
      android:layout_width="fill_parent" /> 
    </TableLayout> 

回答

2

android:layout_toLeftOfandroid:layout_toRightOfandroid:layout_below是與RelativeLayout的使用屬性。您沒有RelativeLayout。因此,上述XML中使用的那些屬性對你沒有好處。

要創建一個包含四列的行,請使用TableRow小部件的四個子項。

我不完全清楚你將如何實現你的右對齊的效果。可能android:gravity會讓你在那裏,但我懷疑問題比這更復雜。

+0

現在不是我的4個佈局項目TableRow的子項嗎? – 2010-09-26 06:18:35

+0

@Sheehan Alam:不可以。請注意您的''標籤的位置。 – CommonsWare 2010-09-26 13:04:16

0

android:gravity應該是你需要的全部,因爲你的TextView已經在width=fill_parent,例如,它會將文本與視圖中的右側對齊。

否則,你也可以玩android:layout_gravity="right",例如,如果您有width=wrap_content的視圖,並且您需要告訴容器如何對齊它。