【问题标题】:Android: How to create such layout dynamicaly (not by xml)?Android:如何动态创建这样的布局(不是通过 xml)?
【发布时间】:2012-01-10 04:29:19
【问题描述】:

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

那么如何实现呢?

我做了类似下面的代码:

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

    } 
}

所以请指导我,我还需要做什么才能创建这样的视图? 谢谢。

【问题讨论】:

    标签: android xml android-layout android-emulator xml-layout


    【解决方案1】:

    尝试使用 TableLayout 和 TableRow 而不是 LinearLayout。

    您也可以在 xml 中创建 TableLayout,我认为它是静态的并添加 TableRows。要在 Java 中使用创建 TableLayout,

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

    【讨论】:

    • 您也可以在 xml 中创建 TableLayout,我认为它是静态的,并在创建后添加 TableRows。
    • @ji tendra:我得到了视图,但是我在 xml 中所做的其他任何重力和标题呢?请帮助我通过java代码实现它。
    • 我想动态创建整个布局。并且基于 for 循环,整个布局将创建与 for 循环运行一样多的时间。
    • 您可以使用 view.setGravity(Gravity.CENTER) 等设置任何视图的重力,但如果您有其他问题,请告诉我您的确切问题以及可能的代码。
    • 我不想为单行循环。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 1970-01-01
    • 2011-05-02
    相关资源
    最近更新 更多