2011-11-04 25 views
0

幾個月前我開始寫一個超簡單的文本冒險遊戲,現在我剛剛回過頭來。但在看過佈局之後,我現在覺得這真的很難看。基於XML的文件格式醜陋佈局的問題

對於整個XML佈局格式,我真的很陌生,我想知道是否有人可以爲我看看這個,讓我知道是否有更好,更有吸引力的編寫這種佈局的方式。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<TableLayout android:stretchColumns="*" 
    android:layout_height="wrap_content" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent"> 

     <TableRow 
      android:layout_height="wrap_content" 
      android:id="@+id/tableRow1" 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:gravity="center"> 

      <TextView android:text="" 
       android:id="@+id/introText" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="2dip" 
       android:layout_marginRight="2dip" 
       android:layout_marginTop="2dip" 
       android:layout_marginBottom="2dip"> 
      </TextView> 
     </TableRow> 

     <TableRow 
      android:layout_height="wrap_content" 
      android:id="@+id/tableRow1" 
      android:layout_weight="1" 
      android:layout_width="fill_parent" 
      android:gravity="center"> 

      <TextView android:text="navigationText" 
       android:id="@+id/navigationText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dip"/> 

      <TextView android:text="actionText" 
       android:id="@+id/actionText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dip"/> 

      <TextView android:text="encounterText" 
       android:id="@+id/encounterText" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_margin="2dip" 
       android:layout_marginBottom="1dip"/> 
     </TableRow> 
</TableLayout> 

<TableLayout android:stretchColumns="*" 
    android:layout_height="wrap_content" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent"> 
    <TableRow 
     android:layout_height="wrap_content" 
     android:id="@+id/tableRow1a" 
     android:layout_weight="1" 
     android:layout_width="fill_parent" 
     android:gravity="left">  

     <TextView android:text="characterInfo" 
      android:id="@+id/charText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip"> 
     </TextView> 
     <TextView android:text="" android:id="@+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"> 
</TextView> 

     <Button android:text="North" 
      android:id="@+id/btnNorth" 
      android:layout_height="wrap_content" 
      android:layout_width="fill_parent" android:layout_margin="2dip"> 
     </Button> 
    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tableRow2" 
     android:gravity="left"> 

     <TextView android:text="statsText" 
      android:id="@+id/statsText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:maxLines="5" 
      android:layout_margin="2dip"> 
     </TextView> 

     <Button android:text="West" 
      android:id="@+id/btnWest" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="32dip" android:layout_marginRight="2dip" 
android:layout_marginTop="2dip" android:layout_marginBottom="2dip"> 
     </Button> 
     <Button 
      android:text="i" 
      android:id="@+id/btnInventory" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_marginLeft="10dip" 
android:layout_marginRight="10dip" android:layout_marginTop="2dip" 
android:layout_marginBottom="2dip"> 
     </Button> 
     <Button 
      android:text="East" 
      android:id="@+id/btnEast" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="2dip" android:layout_marginRight="32dip" 
android:layout_marginTop="2dip" android:layout_marginBottom="2dip"> 
     </Button> 
    </TableRow> 

    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tableRow3" 
     android:gravity="left"> 

     <TextView android:text="itemsText" 
      android:id="@+id/itemsText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip"> 
     </TextView> 
     <TextView android:text="" android:id="@+id/textView2" 
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>  
     <Button android:text="South" 
      android:id="@+id/btnSouth" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="2dip"> 
     </Button> 
    </TableRow> 
    <TableRow 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/tableRow4" 
     android:gravity="center"> 
     <TextView android:text="" android:id="@+id/textView3" 
android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
     <Button android:text="Attack" android:id="@+id/btnFight" 
android:layout_height="wrap_content" android:layout_width="wrap_content" 
android:layout_marginLeft="15dip" android:layout_marginRight="10dip" 
android:layout_marginTop="2dip"></Button> 
     <Button android:text="Flee" android:id="@+id/btnRun" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_marginLeft="10dip" android:layout_marginRight="10dip" 
android:layout_marginTop="2dip"></Button> 
     <Button android:text="Search" android:id="@+id/btnSearch" 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:layout_marginLeft="10dip" android:layout_marginRight="15dip" 
android:layout_marginTop="2dip"></Button> 
    </TableRow> 

</TableLayout> 


</LinearLayout> 

回答

2

如果你只是希望讓您的佈局更便於管理,也有一些簡單的解決方案,我能想到的。他們大多需要零到很小的設計更改。

  • 您可以添加XML註釋獨立的邏輯部分,醜陋的,因爲他們可能是
  • 您可以將您的佈局到單獨的包含的佈局
  • 您可以使用fragments(使用可通過該片段兼容包SDK更新程序(如果需要的話))將您的活動分成單獨的UI組件,這些組件將擁有自己的佈局文件。這是唯一需要移動你的java代碼的解決方案,但我強烈推薦它;它使得UI組件具有高度可重用性。
1

所以我猜你的意思是讓代碼看起來更好,對嗎?你已經提出了一個普遍的問題,所以我能做的最好的是給你一個普遍的答案。你應該做的是將這些佈局元素結合到一起,稱爲CompoundControls。這將允許你將一堆代碼分解爲更小的xml佈局文件。你將基本上只是創建自己的複合小部件,然後可以使用較少的代碼放置在主佈局中。另外,您可以在多個地方重複使用小部件。

+0

我想我承認我完全不瞭解這種XML佈局格式。我來自那些我可以修改HTML和CSS的網站......但是使用XML修改佈局差不多(確實如此)似乎很乏味。謝謝! – SkyeBoniwell

1

假設您希望使代碼看起來更好,最新版本的Eclipse has an XML layout code formatter的ADT插件將您的代碼重新格式化爲普遍接受的Android風格。我發現它生成的代碼看起來相當不錯(至少和XML看起來一樣好)。