Android上如何动态添加LinearLayout?

发布于 2025-01-01 01:06:24 字数 72 浏览 5 评论 0原文

我有一个长度为 n 的数组,现在需要创建 n 个 LinearLayout 并在每个布局上添加不同的内容。 怎样才能动态地完成呢?

I have an array of length n, I now need to create n number of LinearLayouts and add different stuffs on each of them.
How can it be done dynamically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静水深流 2025-01-08 01:06:24
LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);
披肩女神 2025-01-08 01:06:24

最简单的方法是在 xml 中创建布局并使用

LayoutInflater.from(context).inflate(R.layout.my_linear_layout);

对其进行扩充,您可能还需要 setId() code> 您添加的视图,以便您稍后可以轻松访问它们。

The easiest way is to create a layout in xml and inflate it using

LayoutInflater.from(context).inflate(R.layout.my_linear_layout);

You may also want to setId() your added views so you can access them easily later on.

吝吻 2025-01-08 01:06:24

我使用RelativeLayout解决了这个问题,我发现它更容易使用。是的,当然就像上面指出的那样,我使用了 setId()。这是我实现的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView sv = new ScrollView(this);

    //Parent RelativeLayout
    parentLayout = new RelativeLayout(this);
    parentLayout.setBackgroundColor(Color.WHITE);
    params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    parentLayout.setLayoutParams(params);
    sv.addView(parentLayout);

    final String[] comList = getCommunication();
    int listLength=0;
    try{
    listLength= comList.length/3;
    }catch(Exception e){System.out.println(e);System.exit(0);}

    childLayout= new RelativeLayout[listLength] ;
    TextView[] tvName  = new TextView[listLength];
    TextView[] tvDate  =new TextView[listLength];
    TextView[] tvMsg =new TextView[listLength];

    for(int i =0;i<listLength;i++){
        try{

        childLayout[i] = new RelativeLayout(this);
        childLayout[i].setPadding(5, 5, 5, 5);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
        if(i==0){params.addRule(RelativeLayout.BELOW);}
        else{params.addRule(RelativeLayout.BELOW,i);}
        childLayout[i].setId(i+1);
        childLayout[i].setClickable(true);
        childLayout[i].setLayoutParams(params);
        childLayout[i].setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {


                //Create the intent
                  Intent i = new Intent("ACTIIVTY");
                   startActivity(i);
            }       
        });

        tvName[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        tvName[i].setLayoutParams(params);
        childLayout[i].addView(tvName[i]);
        if(comList[i*3].length()>24){
            String name = comList[i*3].substring(0,24)+"...";
            tvName[i].setText(name);
        }else{
            tvName[i].setText(comList[i*3]);
        }
        tvName[i].setId(listLength+1+i);
        tvName[i].setTextSize(12);
        tvName[i].setTextColor(Color.BLACK);

        tvDate[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tvDate[i].setLayoutParams(params);
        childLayout[i].addView(tvDate[i]);
        tvDate[i].setTextSize(11);
        tvDate[i].setTextColor(Color.BLUE);
        tvDate[i].setText(comList[i*3+1]);


        tvMsg[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, listLength+1+i);
        tvMsg[i].setLayoutParams(params);
        childLayout[i].addView(tvMsg[i]);
        tvMsg[i].setTextSize(11);
        tvMsg[i].setTextColor(Color.GRAY);
        if(comList[i*3+2].length()>96){
            String msg = comList[i*3+2].substring(0,96)+"...";
            tvMsg[i].setText(msg);
        }else{
            tvMsg[i].setText(comList[i*3+2]);
        }

        parentLayout.addView(childLayout[i]);

        }catch(Exception e){System.out.println("Errrorrrrr");}
    }

    setContentView(sv);
}

I solved it using RelativeLayout which I found a little easier to work with. Yes of-course like the guys pointed out above I used setId(). Here is the code I implemented:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView sv = new ScrollView(this);

    //Parent RelativeLayout
    parentLayout = new RelativeLayout(this);
    parentLayout.setBackgroundColor(Color.WHITE);
    params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    parentLayout.setLayoutParams(params);
    sv.addView(parentLayout);

    final String[] comList = getCommunication();
    int listLength=0;
    try{
    listLength= comList.length/3;
    }catch(Exception e){System.out.println(e);System.exit(0);}

    childLayout= new RelativeLayout[listLength] ;
    TextView[] tvName  = new TextView[listLength];
    TextView[] tvDate  =new TextView[listLength];
    TextView[] tvMsg =new TextView[listLength];

    for(int i =0;i<listLength;i++){
        try{

        childLayout[i] = new RelativeLayout(this);
        childLayout[i].setPadding(5, 5, 5, 5);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
        if(i==0){params.addRule(RelativeLayout.BELOW);}
        else{params.addRule(RelativeLayout.BELOW,i);}
        childLayout[i].setId(i+1);
        childLayout[i].setClickable(true);
        childLayout[i].setLayoutParams(params);
        childLayout[i].setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {


                //Create the intent
                  Intent i = new Intent("ACTIIVTY");
                   startActivity(i);
            }       
        });

        tvName[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        tvName[i].setLayoutParams(params);
        childLayout[i].addView(tvName[i]);
        if(comList[i*3].length()>24){
            String name = comList[i*3].substring(0,24)+"...";
            tvName[i].setText(name);
        }else{
            tvName[i].setText(comList[i*3]);
        }
        tvName[i].setId(listLength+1+i);
        tvName[i].setTextSize(12);
        tvName[i].setTextColor(Color.BLACK);

        tvDate[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        tvDate[i].setLayoutParams(params);
        childLayout[i].addView(tvDate[i]);
        tvDate[i].setTextSize(11);
        tvDate[i].setTextColor(Color.BLUE);
        tvDate[i].setText(comList[i*3+1]);


        tvMsg[i] = new TextView(this);
        params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.BELOW, listLength+1+i);
        tvMsg[i].setLayoutParams(params);
        childLayout[i].addView(tvMsg[i]);
        tvMsg[i].setTextSize(11);
        tvMsg[i].setTextColor(Color.GRAY);
        if(comList[i*3+2].length()>96){
            String msg = comList[i*3+2].substring(0,96)+"...";
            tvMsg[i].setText(msg);
        }else{
            tvMsg[i].setText(comList[i*3+2]);
        }

        parentLayout.addView(childLayout[i]);

        }catch(Exception e){System.out.println("Errrorrrrr");}
    }

    setContentView(sv);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文