2010-07-11 60 views
0

Im從我的手機發布,所以請原諒愚蠢的錯別字和格式問題。垂直對齊按鈕和垂直定向LinearLayout內的垂直定向LinearLayout的問題

我有一個列出玩家可以加載的已保存遊戲的活動。

我創建了一個簡單的佈局xml文件,它定義了一個ScrollView。在加載時,我抓取所有保存的遊戲並以編程方式爲每個保存的遊戲添加一個視圖給ScrollView的垂直定向的LinearLayout子項。

每個遊戲的視圖由一個水平方向的LinearLayout組成,後者又包含一個Button和一個垂直方向的LinearLayout。該LinearLayout反過來包含一些TextViews和ImageViews(還有一個LinearLayout,爲了清晰起見我在這裏省略)。

層次結構看起來像這樣(有些細節省略)。

ScrollView 
     LinearLayout - vertical 
      Each saved game: 
      LinearLayout - horizontal 
       Button - load game 
       LinearLayout - vertical 
        TextView - game name 
        TextView - date string 

我的問題:

我想按鈕的頂部和「遊戲名稱」 texview被垂直排列,但是TextView的(或者也許是的LinearLayout父母)有一些流氓填充上我無法擺脫的頂端。細節請參見屏幕截圖。

alt

LoadSaved類: 注:mScrollView是嚴重命名。它指向ScrollView的子LinearLayout。

public class LoadSaved extends Activity { 
    public LinearLayout mScrollView; 
    private MinerDb mDb; 

     public void onCreate(Bundle b) { 
      super.onCreate(b); 
      setContentView(R.layout.loadsaved); 
      mDb = new MinerDb(this); 

      mScrollView = (LinearLayout) findViewById(R.id.load_scroll_view); 
      Bundle[] savedGames = mDb.getSavedGames(); 

      for (int i = 0; i < savedGames.length; i++) { 
       Bundle game = savedGames[i]; 

       final int gameId = game.getInt("gameId"); 
       String name = game.getString("name"); 
       String date = game.getString("date"); 

       Bundle player = game.getBundle("player"); 
       int playerMoney = player.getInt("money"); 
       int playerHealth = player.getInt("health"); 

       LinearLayout gameContainer = new LinearLayout(getApplicationContext()); 
       gameContainer.setPadding(5, 5, 5, 5); 
       gameContainer.setGravity(Gravity.TOP); 
       gameContainer.setOrientation(LinearLayout.HORIZONTAL); 
       gameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       Button loadButton = new Button(getApplicationContext()); 
       loadButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       loadButton.setText("Load"); 


       LinearLayout gameInfo = new LinearLayout(getApplicationContext()); 
       gameInfo.setOrientation(LinearLayout.VERTICAL); 
       gameInfo.setPadding(10,0,10,10); 
       gameInfo.setGravity(Gravity.TOP); 
       gameInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

       TextView nameView = new TextView(getApplicationContext()); 
       nameView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       nameView.setGravity(Gravity.TOP); 
       nameView.setText(name); 

       TextView dateView = new TextView(getApplicationContext()); 
       dateView.setPadding(5,0,0,0); 
       dateView.setGravity(Gravity.TOP); 
       dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
       dateView.setText(date); 

       LinearLayout playerView = new LinearLayout(getApplicationContext()); 
       playerView.setOrientation(LinearLayout.HORIZONTAL); 
       playerView.setPadding(5,0,0,0); 
       playerView.setGravity(Gravity.TOP); 
       playerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

       TextView playerMoneyView = new TextView(getApplicationContext()); 
       playerMoneyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       playerMoneyView.setPadding(0,0,10,0); 
       playerMoneyView.setTextColor(Color.GREEN); 
       playerMoneyView.setText("$" + playerMoney); 

       TextView playerHealthView = new TextView(getApplicationContext()); 
       playerHealthView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
       playerHealthView.setPadding(0,0,10,0); 
       playerHealthView.setTextColor(Color.RED); 
       playerHealthView.setText(playerHealth + "%"); 

       playerView.addView(playerMoneyView); 
       playerView.addView(playerHealthView); 

       gameInfo.addView(nameView); 
       gameInfo.addView(dateView); 
       gameInfo.addView(playerView); 

       gameContainer.addView(loadButton); 
       gameContainer.addView(gameInfo); 

       mScrollView.addView(gameContainer); 

       loadButton.setOnClickListener(new OnClickListener() { 
        public void onClick(View v) { 
          Log.e("LoadSaved", "LoadSaved::onCreate: Clicking: " + gameId); 
          Intent loadGameIntent = new Intent(LoadSaved.this, Miner.class); 
          loadGameIntent.putExtra("load_game", gameId); 
          startActivity(loadGameIntent); 
          finish(); 
         } 
       }); 
      } 
     } 
    } 

loadsaved.xml

  <LinearLayout 
       android:orientation="vertical" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/load_scroll_view" /> 

     </ScrollView> 
    </LinearLayout> 

回答

1

對提示ListView和RelativeLayout的答案+1。對於這種情況,你可能需要一個帶有使用RelativeLayout的項目佈局的ListView。 (如果有很多項目,ListView的縮放會更好,如果這是一個保存的遊戲列表,它似乎會增長很多)。對於這種類型的UI,建議讓整行/列表項可點擊比使用小負載按鈕,但這是一個設計問題,最終取決於你。

請勿使用getApplicationContext來創建您的視圖。活動是一個上下文,只是通過this你的情況。

默認情況下,LinearLayouts嘗試通過其文本基線來對齊子視圖(如果存在)。請注意,按鈕的加載文本底部與截圖中的CURRENT_GAME文本完全對齊。嘗試gameContainer.setBaselineAligned(false)

通常,gameInfo佈局只會在設置baselineAlignedChildIndex時報告其子項的基線,但看起來像通過編程創建LinearLayouts時,此行爲在cupcake和eclair之間發生了變化。 (鏈接到在AOSP here中更改它的提交。)

+0

回覆:將「this」傳遞給上下文。似乎當我這樣做時,我終於在各處泄漏了記憶。然後在開始一些活動後,它崩潰。使用getApplicationContext內存消耗保持穩定。 – 2010-07-12 17:00:17

+0

所有的答案都很好(我很高興我學到了一點關於RelativeLayout的知識),但你的答案工作最簡單。謝謝。 – 2010-07-12 17:14:02

+0

您必須小心將活動作爲上下文傳遞的位置,但使用它構建視圖應該沒有問題,除非您以這種方式存儲對這些視圖的引用,以致它們會持續超出活動的壽命。如果你這樣做,你可能會遇到其他問題。 – adamp 2010-07-13 02:41:40

3

如果你想要的任何一種對齊的,你爲什麼不使用RelativeLayout的?這基本上是爲了將一個視圖與另一個視圖對齊。 android:layout_alignTop聽起來像你想要的東西。

(而且,當然,驗證填充值在所有控件一樣,但我敢肯定,你這樣做。)

+0

謝謝。我看看那個。這是我的第一個Android項目,所以我對這個平臺有些不滿。 – 2010-07-11 20:53:57

+0

是的,讓佈局按你想要的方式工作,一開始就需要一些練習,但是一旦你掌握了它,你就會喜歡它。對於超出簡單元素列表的任何事物,RelativeLayout通常是您的最佳選擇。 – EboMike 2010-07-11 20:55:12

+0

這看起來像我的解決方案。我現在正在一家酒吧編程,所以重寫我的觀點可能需要一段時間......啤酒正在付出代價。一旦我找到了,我會標記你正確的。我想明天發佈,所以我可能會等待,直到我發佈後進行初步更新以解決此問題。 – 2010-07-11 21:42:42

2

你爲什麼不嘗試使用ListView的那種貴的。

您仍然需要定義一行xml。