2014-02-17 3 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, либо создать несколько горизонтальных LinearLayouts внутри вертикального LinearLayout.

0

Привет, как упоминалось ранее, это можно сделать, используя «Табличный макет» или «Макет сетки». Но я также предлагаю вам использовать вложенные простые линейные и горизонтальные макеты. Это обеспечит поддержку вашего макета даже в телефонах со старой версией ОС Android. Поскольку для Grid Layout требуется 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> 
Смежные вопросы