Android: Как создать такой макет динамически (не с помощью xml)? - PullRequest
0 голосов
/ 10 января 2012

В моем приложении я хочу создать макет, как показано ниже:

enter image description here

Так как сделать это возможным?

Я сделал что-то вроде следующего кода:

public void showResult()
{

    List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> textListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    List<TextView> imageListAnswer = new ArrayList<TextView>(tempEmployerList.size());
    for(int i = 0; i<=tempEmployerList.size()-1; i++)
    {    
        LinearLayout innerLayout = new LinearLayout(this);
        innerLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        innerLayout.setBackgroundColor(0x00000000);

        // set the Multiple TextView
        TextView mHeading = new TextView(getApplicationContext());
        TextView middleValue = new TextView(getApplicationContext());
        TextView aImageView = new TextView(getApplicationContext());

        mHeading.setText("\n"+"1");
        //mHeading.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        mHeading.setTextColor(0xFFFF0000);
        mHeading.setPadding(3, 0, 0, 0);


        middleValue.setText("\n"+"2");
        //middleValue.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        middleValue.setTextColor(0xFFFF0000);
        middleValue.setGravity(Gravity.RIGHT);
        middleValue.setPadding(2, 0, 9, 0);

        aImageView.setText("\n"+"3");
        //aImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        aImageView.setGravity(Gravity.RIGHT);
        aImageView.setTextColor(0xFF000000);
        aImageView.setPadding(0, 0, 9, 0);

        View line = new View(getApplicationContext());
        //line.setOrientation(1);
        line.setLayoutParams(new LayoutParams(2, android.view.ViewGroup.LayoutParams.FILL_PARENT)); 
        line.setBackgroundColor(0xFF000000); 

        /**** Any other text view setup code ****/    

        innerLayout.addView(mHeading);
        innerLayout.addView(middleValue);
        innerLayout.addView(aImageView);
        innerLayout.addView(line);
        myLinearLayout.addView(innerLayout);  

        textListWord.add(mHeading); 
        textListAnswer.add(middleValue);
        imageListAnswer.add(aImageView);

    } 
}

Так что, пожалуйста, объясните мне, что еще мне нужно сделать, чтобы создать такое представление? Спасибо.

1 Ответ

1 голос
/ 10 января 2012

попробуйте TableLayout и TableRow вместо LinearLayout.

Вы также можете создать TableLayout в xml, так как он, я думаю, будет статичным и добавить TableRows.Чтобы создать TableLayout в Java, используйте

TableLayout tblLayout=new TableLayout(this); 

Установите LayoutParams и другие свойства макета таблицы в Java, как я устанавливаю LayoutParams:

LayoutParams params=new LayoutParams(LayoutParams.Fill_Parent, LayoutParams.Wrap_Content); 
tblLayout.setLayoutParams(params);

Создайте цикл для создания TableRows ивставка:

for(int i=0;i<arrList1.size();i++)
{
   TableRow row=new TableRow(this);
   row.setLayoutParams(params);

   TextView lbl1=new TextView(this);
   lbl1.setText(arrList1.get(i));
   row.addView(lbl1);

   TextView lbl2=new TextView(this);
   lbl2.setText(arrList2.get(i));
   row.addView(lbl2);

   TextView lbl3=new TextView(this);
   lbl3.setText(arrList3.get(i));
   row.addView(lbl3);

   tblLayout.addView(row);
}
...