2012-07-13 179 views
1

在我的應用程序中,我顯示了一個表格佈局,其中我在一行中對齊了4個文字查看。桌面佈局textview對齊

我的表佈局:(我不知道很多優秀的設計,這怎麼)

<TableLayout 
    android:id="@+id/tablelayout" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:paddingRight="2dip" 
    android:stretchColumns="4"> 
    <TableRow > 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Income" /> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Expense" 
      android:layout_marginLeft="-150dp" /> 
    </TableRow>  
    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <View 
      android:id="@+id/line1" 
      android:layout_width="wrap_content" 
      android:layout_height="1dip" 
      android:layout_weight="1" 
      android:background="#FF909090" 
      android:padding="2dip" /> 
    </TableRow> 
    <TableRow 
     android:id="@+id/tablerowhouse" 
     android:layout_marginTop="30px"> 
     <TextView 
      android:id="@+id/text40" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Household:" />    
     <TextView    
      android:id="@+id/houseinctot" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Household:" 
      android:layout_marginLeft="-250dp" /> 
     <TextView 
      android:id="@+id/text41" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Household:" 
      android:layout_marginLeft="-150dp" />   
     <TextView 
      android:id="@+id/houseexptot" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Household:" 
      android:layout_marginLeft="-70dp" 
      android:gravity="right"/> 
    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     <View 
      android:id="@+id/line1" 
      android:layout_width="wrap_content" 
      android:layout_height="1dip" 
      android:layout_weight="1" 
      android:background="#FF909090" 
      android:padding="2dip" /> 
    </TableRow> 
    <TableRow 
     android:layout_marginTop="30px"> 
     <TextView 
      android:id="@+id/text42" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Travel:" 
      android:layout_span="3" />  
     <TextView 
      android:id="@+id/trlinctot" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Travel" 
      android:layout_marginLeft="-250dp" 
      android:layout_alignParentRight="true" /> 
     <TextView android:id="@+id/text43" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Travel:" 
      android:layout_marginLeft="-150dp" />   
     <TextView    
      android:id="@+id/trlexptot" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Travel:" 
      android:layout_marginLeft="-70dp" 
      android:gravity="right" />  
    </TableRow> 
    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
     <View 
      android:id="@+id/line1" 
      android:layout_width="wrap_content" 
      android:layout_height="1dip" 
      android:layout_weight="1" 
      android:background="#FF909090" 
      android:padding="2dip" /> 
    </TableRow> 


    <TableRow android:layout_marginTop="30px"> 
<TextView android:id="@+id/text44" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_span="4"> 
    </TextView>  
     <TextView 
      android:id="@+id/eduinctot" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education" android:layout_marginLeft="-250dp" android:layout_alignParentRight="true"> 
    </TextView> 

      <TextView android:id="@+id/text45" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-150dp" > 
    </TextView>    
     <TextView    
      android:id="@+id/eduexptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Education:" android:layout_marginLeft="-70dp" android:gravity="right" 
      > 
    </TextView>  
</TableRow> 

     <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <View 
       android:id="@+id/line1" 
       android:layout_width="wrap_content" 
       android:layout_height="1dip" 
       android:layout_weight="1" 
       android:background="#FF909090" 
       android:padding="2dip" /> 
     </TableRow> 

     <TableRow android:layout_marginTop="30px"> 
<TextView android:id="@+id/text46" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_span="4"> 
    </TextView>  
     <TextView 
      android:id="@+id/grp4inctot" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4" android:layout_marginLeft="-250dp"> 
    </TextView> 

      <TextView android:id="@+id/text47" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group4:" android:layout_marginLeft="-150dp" > 
    </TextView>    
     <TextView    
      android:id="@+id/grp4exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group4:" android:layout_marginLeft="-70dp" android:gravity="right" 
      > 
    </TextView>  
</TableRow> 

      <TableRow 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" > 

      <View 
       android:id="@+id/line1" 
       android:layout_width="wrap_content" 
       android:layout_height="1dip" 
       android:layout_weight="1" 
       android:background="#FF909090" 
       android:padding="2dip" /> 
     </TableRow> 

      <TableRow android:layout_marginTop="30px"> 
<TextView android:id="@+id/text48" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_span="4"> 
    </TextView>  
     <TextView 
      android:id="@+id/grp5inctot" 
      android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5" android:layout_marginLeft="-250dp"> 
    </TextView> 

      <TextView android:id="@+id/text49" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Group5:" android:layout_marginLeft="-150dp" > 
    </TextView>    
     <TextView    
      android:id="@+id/grp5exptot" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="group5:" android:layout_marginLeft="-70dp" android:gravity="right" 
      > 
    </TextView>  
</TableRow> 

現在我想的是:

textview1:value(textview2)  textview3:value(textview4) 
textview5:value(textview6)  textview7:value(textview8) . 

我應該右鍵allign textview2和textview6(與在屏幕的一半),並應該對齊textview6和textview8

What i have:       What i want: 
    12345 4565       12345 4565 
    12  56       12  56 
    56  44       56  44 
    6   6        6  6 
+0

爲什麼你又發佈了同樣的問題? – 2012-07-13 07:06:06

+0

其實我錯誤地發佈在前面的問題(XML文件)。我也編輯了前面的問題,但沒有迴應。所以只有Dipak Keshariya..Because我想知道這個答案。我有任何答案.. – 2012-07-13 07:09:11

+0

張貼全部xml文件,如果可能的話刪除以前的問題。 – 2012-07-13 07:10:56

回答

0

你寫所有Textview中的屬性。

+0

沒有hardik joshi.If我使用上述屬性意味着textview2將與textview4重疊,textview6與textview8重疊 – 2012-07-13 06:44:15

0

如果您有ALIGN_PARENT_RIGHT選項,則應使用relative layout

像這樣的事情

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:paddingLeft="5dip" 
    android:paddingRight="5dip" 
    android:paddingTop="8dip" 
    android:paddingBottom="8dip" 
> 

<TextView 
    android:id="@+id/tv_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:text="turn on" 
/> 

<CheckBox 
    android:id="@+id/chekcbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    /> 
</RelativeLayout> 

android:layout_alignParentRight="true"會將你textview到最右邊。 只要嘗試一次,它爲我工作。

+0

您可以將textview2放置到textview1右側,並且還使用'android:layout_alignParentRight =「true」'將您的textview極端的權利。 「TableLayout」的每一行都可以是「RelativeLayout」。或者你可以在每行都有'ListView'作爲'RelativeLayout'.See我的編輯完整的xml代碼 – Abhilasha 2012-07-13 07:01:55

+0

沒有冒犯。但是你是否嘗試在代碼中放置該行?我想這比猜測它會導致什麼好得多。它肯定會節省你很多時間。只需粘貼代碼,看看它在圖形佈局中的外觀如何...讓您的生活變得輕鬆! :)或者可能會超過你的完整XML,我們可以建議更好的東西。 – Abhilasha 2012-07-13 07:14:42

+0

Abhilasha ..我嘗試了我所說的即將到來的...如果只有2行textview意味着v使它作爲左1和另一個right.But我有4 textview如何使v左對齊和右對齊? – 2012-07-13 07:26:52

0

試試這個layout.This的作品。

<?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:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_label" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="123456" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_amount" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="1234" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_amount1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="123456" /> 
     </RelativeLayout> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_label1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="16" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_amount3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="1" /> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="100dp" 
      android:layout_height="wrap_content" > 

      <TextView 
       android:id="@+id/summary_amount4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentRight="true" 
       android:text="1" /> 
     </RelativeLayout> 
    </LinearLayout> 

</LinearLayout> 
+0

你試過嗎? – Abhilasha 2012-07-13 11:22:18