2014-02-17 47 views
0

我想在LinearLayout中創建一系列按鈕。所以,我有以下的代碼Android動態按鈕在LinearLayout內

XML

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

在我的活動

LinearLayout yearContainer=(LinearLayout)findViewById(R.id.yearContainer); 
for(int i=0;i<16;i++){ 
Button btn=new Button(this); 
    btn.setText("Button "+i); 
    btn.setId(150+i); 

    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(width/3,LayoutParams.WRAP_CONTENT); 
    btn.setLayoutParams(params); 

    yearContainer.addView(btn); 

} 

但按鍵被垂直排列。我需要它作爲以下模式。

enter image description here

我是新到Android。請指教

在此先感謝

+0

使用TableLayout – Raghunandan

回答

1

你不能做什麼喲想用一個單一的LinearLayout。您需要使用TableLayout,或者在垂直LinearLayout中創建多個Horizo​​ntal LinearLayout。

0

你好,如前所述,這可以使用「表格佈局」或「網格佈局」來完成。但我也會建議你使用嵌套的簡單線性和水平佈局。這將確保即使在使用舊版Android OS的手機中也能支持您的佈局。由於網格佈局需要API等級14或更高。

所以下面的代碼應該可以讓您得到您期望的簡單佈局結果。

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="4" > 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      android:weightSum="4" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="1" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="2" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="3" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="4" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="4" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="5" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="6" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="7" /> 

      <Button 
       android:id="@+id/btn_Sampleact_H" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="8" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="4" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="9" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="10" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="11" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="12" /> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      android:weightSum="4" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="13" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="14" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="15" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="16" /> 
     </LinearLayout> 
    </LinearLayout>