2014-02-20 1 views
1

У меня есть следующий код:Добавление изображения кнопок соседствуют динамически Liner Layout, ориентация вертикальная

LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout); 

for(i=0;i<=3 ;i++) { 
    ImageButton ib= new ImageButton(this); 
    BitmapDrawable imagebd; 
    ib.setClickable(true); 
    imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName()); 

    ib.setBackgroundResource(imageid); 

    lyt.addView(ib); 
} 

все изображения показывают вертикально, я хочу, чтобы это было горизонтальным

+0

добавить то внутри новой линейной компоновки – Shahar

ответ

0

установки Избегайте атрибутов в ваш код Java, когда это можно сделать через XML. Попробуйте установить атрибут orientation в вашем ListView:

<LinearLayout 
    ... 
    android:orientation="horizontal" 
    ...> 
</LinearLayout> 
0

Попробуйте этот код:

LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout); 
lyt.setOrientation(LinearLayout.HORIZONTAL); 
for(i=0;i<=3 ;i++) { 
    ImageButton ib= new ImageButton(this); 
    BitmapDrawable imagebd; 
    ib.setClickable(true); 
    imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName()); 

    ib.setBackgroundResource(imageid); 

    lyt.addView(ib); 
} 
0

Try это так:

LinearLayout lyt = (LinearLayout)findViewById(R.id.mylayout); 

LinearLayout buttonsLinearLayout = new LinearLayout(context); 
buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

for(i=0;i<=3 ;i++) 
{ 
    ImageButton ib= new ImageButton(this); 
    BitmapDrawable imagebd; 
    ib.setClickable(true); 
    imageid = getResources().getIdentifier("drawable/" + image,null,getPackageName()); 

    ib.setBackgroundResource(imageid); 

    buttonsLinearLayout.addView(ib); 
} 
lyt.addView(buttonsLinearLayout); 

Таким образом, вы все еще есть основной вертикальный LinearLayout, поэтому вы можете помещать вещи под кнопки.

EDIT: использовать его для более чем 1 ряд, я сделал простую математическую выч ...

LinearLayout lyt = (LinearLayout) findViewById(R.id.mylayout); 

    // calculate the number of rows needed 
    int numOfButtons = something you already have i guess; 

    // row layout 
    LinearLayout buttonsLinearLayout = new LinearLayout(context); 
    ; 

    for (i = 0; i <= numOfButtons; i++) { 

     // for every 3 rows, create a new layout 
     // and add it to the main linear layout 
     if (i % 3 == 0) { 
      // create layout for a 3 button row 
      buttonsLinearLayout = new LinearLayout(context); 
      buttonsLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 

      // add the new row with 3 buttons to the main lineal layout 
      lyt.addView(buttonsLinearLayout); 
     } 

     ImageButton ib = new ImageButton(this); 
     BitmapDrawable imagebd; 
     ib.setClickable(true); 
     imageid = getResources().getIdentifier("drawable/" + image, null, getPackageName()); 

     ib.setBackgroundResource(imageid); 
     buttonsLinearLayout.addView(ib); 
    } 


} 

не проверял, дайте мне знать, если это работает для вас ...

+0

это работает, но дело в том, что они имеют от 10 до 15 кнопок image.it должны соответствовать ширине экрана 3 изображения на строку, а следующее изображение должно начинаться со следующей строки – touseef

+0

каждая кнопка изображения имеет одинаковую ширину? – Shahar

+0

да он имеет такую ​​же ширину и высоту – touseef

Смежные вопросы