2014-04-14 38 views
2

我有一個方法,它接受一串文本,將它切出來,並使字符串中的所有單詞的按鈕。我添加這些到水平線性佈局是這樣的:如何讓我的按鈕適合Android屏幕?

for (int x = 0; i < string.length; x++) { 
      Button word = new Button(context); 
      word.setText(string[x]+""); 
      myLinearLayout.addView(word); 
     } 

的問題是,如果字符串太長,按鈕會關閉屏幕。我該如何做到這一點,使按鈕出現在下面一行而不是跳出屏幕?我一直在尋找解決方案整天,但無法找到答案。

謝謝!

+0

用更短?使用字體度量來獲取單詞的總長度或基於所有單詞中字母數量的簡單算法,直到當前迭代並添加換行符(如果其大於X,其中X取決於屏幕寬度)? – tgkprog

回答

1

試試這個..

for (int x = 0; i < string.length; x++) { 
     Button word = new Button(context); 
     word.setText(string[x]+""); 
     LinearLayout.LayoutParams left_on = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1); 
     word.setLayoutParams(left_on); 
     myLinearLayout.addView(word); 
    } 
+0

謝謝,這有助於很多!但是,它只是通過縮小視圖內的按鈕來保持按鈕,而不是換新的一行。我意識到水平線性佈局只能水平堆疊視圖,但我不確定我應該用什麼來代替。 – user3531749

+0

你可以使用http://developer.android.com/reference/android/widget/Horizo​​ntalScrollView.html – velis