2010-04-21 39 views
0

可能我還不明白TableLayout的佈局屬性。似乎沒有可能像HTML一樣實現這樣一個靈活的表格,因爲沒有單元格。我的目標是要實現這樣的佈局:如何將TableLayout中的按鈕對齊到不同的方向?

enter image description here

我怎樣才能做到這一點?我想過使用GridView,但這在XML中似乎不太有用。 我的努力是這樣的:

 <TableLayout 
     android:id="@+id/tableLayout" 
     android:layout_width="320sp" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal" 
     android:gravity="bottom" 
     android:layout_alignParentBottom="true"> 
     <TableRow 
     android:background="#333333" 
     android:gravity="bottom" 
     android:layout_width="fill_parent">  
     <Button 
      android:id="@+id/btnUp" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:gravity="left" 
      android:text="Lift U" 
      /> 
     <Button 
      android:id="@+id/btnScreenUp" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:text="Scrn U" 
      /> 
     </TableRow> 
     <TableRow 
      android:background="#444444" 
      android:gravity="bottom" 
      android:layout_gravity="right"> 
      <Button 
      android:id="@+id/btnDown" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:text="Lift D" 
      /> 
      <Button 
      android:id="@+id/btnScreenLeft" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:text="Scrn L" 
      /> 
      <Button 
      android:id="@+id/btnScreenDown" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:text="Scrn D" 
      /> 
      <Button 
      android:id="@+id/btnScreenRight" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:gravity="right" 
      android:layout_gravity="right" 
      android:text="Scrn R" 
      /> 
     </TableRow> 
</TableLayout> 

回答

7

我找到了解決方案。第二排有4個按鈕。隨着android:stretchColumns =「1」我拉伸第二列,所以對齊已經是我想要的。唯一的問題是按鈕「Scrn U」。它也會被拉長。所以,你必須添加一個不可見的按鈕(這將被拉伸),這個按鈕「按下」按鈕「SCRN U」下一個「列」:

 <TableLayout 
     android:id="@+id/tableLayout" 
     android:layout_width="320sp" 
     android:layout_height="fill_parent" 
     android:stretchColumns="1" 
     android:gravity="bottom" 
     android:layout_alignParentBottom="true"> 
     <TableRow 
     android:background="#333333" 
     android:gravity="bottom">  
     <Button 
      android:id="@+id/btnUp" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:text="Lift U" 
      /> 
     <Button 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:visibility="invisible" 
     /> 
     <Button 
      android:id="@+id/btnScreenUp" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:layout_gravity="right" 
      android:text="Scrn U" 
      /> 
     </TableRow> 
     <TableRow 
      android:background="#444444" 
      android:layout_gravity="right"> 
      <Button 
      android:id="@+id/btnDown" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:text="Lift D" 
      /> 
      <Button 
      android:id="@+id/btnScreenLeft" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:layout_gravity="right" 
      android:text="Scrn L" 
      /> 
      <Button 
      android:id="@+id/btnScreenDown" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:layout_gravity="right" 
      android:text="Scrn D" 
      /> 
      <Button 
      android:id="@+id/btnScreenRight" 
      android:layout_width="60sp" 
      android:layout_height="50sp" 
      android:layout_gravity="right" 
      android:text="Scrn R" 
      /> 
     </TableRow> 
</TableLayout> 
1

未經檢驗的,但是這可能會工作:

<TableLayout 
    android:id="@+id/tableLayout" 
    android:layout_width="320sp" 
    android:layout_height="fill_parent" 
    android:layout_gravity="center_horizontal" 
    android:gravity="bottom" 
    android:layout_alignParentBottom="true"> 
    <TableRow 
    android:background="#333333" 
    android:gravity="bottom" 
    android:layout_width="fill_parent">  
    <Button 
     android:id="@+id/btnUp" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:layout_span="3" 
     android:text="Lift U" 
     /> 
    <Button 
     android:id="@+id/btnScreenUp" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:layout_span="2" 
     android:text="Scrn U" 
     /> 
    </TableRow> 
    <TableRow 
     android:background="#444444" 
     android:gravity="bottom" 
     android:layout_gravity="right"> 
     <Button 
     android:id="@+id/btnDown" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:layout_span="2" 
     android:text="Lift D" 
     /> 
     <Button 
     android:id="@+id/btnScreenLeft" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:text="Scrn L" 
     /> 
     <Button 
     android:id="@+id/btnScreenDown" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:text="Scrn D" 
     /> 
     <Button 
     android:id="@+id/btnScreenRight" 
     android:layout_width="60sp" 
     android:layout_height="50sp" 
     android:text="Scrn R" 
     /> 
    </TableRow> 

基本上,我只是規定了一些細胞有layout_span,這是怎樣的像HTML表格colspan一樣。有了這個,你不必嘗試和對齊等。

+0

你好,對不起 它沒有這方面的工作辦法。它看起來與我以前的努力相似。在你的情況下,第一行包含按鈕「擡起U」,看起來是正確的,但下一個按鈕「Scrn U」被延伸到下一行的下面的按鈕上。此外,下一行中的「Scrn L」按鈕看起來會被裁剪。另外兩個按鈕「Scrn D」,「Scrn R」看起來正確。但是所有按鈕都貼在左側。我以前的努力看起來很相似。我無法管理它將它們中的一些對準右側。 我不知道爲什麼這一個佈局是如此繁瑣。其他佈局看起來相當精心。 – Bevor 2010-04-22 17:23:22