Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:如何创建这样的动态布局(而不是xml)?_Android_Xml_Android Layout_Android Emulator_Xml Layout - Fatal编程技术网

Android:如何创建这样的动态布局(而不是xml)?

Android:如何创建这样的动态布局(而不是xml)?,android,xml,android-layout,android-emulator,xml-layout,Android,Xml,Android Layout,Android Emulator,Xml Layout,在我的应用程序中,我希望创建布局,如下图所示: 那么,如何使之成为可能呢 我做了如下代码: public void showResult() { List<TextView> textListWord = new ArrayList<TextView>(tempEmployerList.size()); List<TextView> textListAnswer = new ArrayList<TextView>(tempEmp

在我的应用程序中,我希望创建布局,如下图所示:

那么,如何使之成为可能呢

我做了如下代码:

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);

    } 
}
public void showResult()
{
List textListWord=newArrayList(tempEmployerList.size());
List textListAnswer=newArrayList(tempEmployerList.size());
List imageListAnswer=newArrayList(tempEmployerList.size());

对于(int i=0;i请尝试TableLayout和TableRow,而不是LinearLayout


您也可以用xml创建TableLayout,我认为它是静态的,可以添加TableRows

TableLayout tblLayout=new TableLayout(this); 
在java中设置LayoutParams和表布局的其他属性,就像我设置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);
}

用于(int i=0;iYou也可以用xml创建TableLayout,因为我认为它是静态的,并且在创建这些TableRows之后添加TableRows。@ji tendra:我得到了视图,但是重力和标题如何?我在xml中所做的任何其他操作?请帮助我通过java代码使之成为可能。我想创建整个布局动态。并基于for循环整个布局创建的时间与for循环运行的时间一样长。您可以使用view.setGravity(gravity.CENTER)等设置任何视图的重力,但如果您有其他问题,请告诉我您的确切问题以及可能的代码。我不希望for循环只运行一行。