簡體   English   中英

如何在Android上動態添加LinearLayout?

[英]How to dynamically add LinearLayout on Android?

我有一個長度為n的數組,我現在需要創建n個LinearLayouts並在每個上添加不同的東西。 怎么能動態完成?

LinearLayout lLayout = new LinearLayout(context);
parentWidget.addView(lLayout);

最簡單的方法是在xml中創建一個布局並使用它進行膨脹

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

您可能還想setId()添加的視圖,以便稍后可以輕松訪問它們。

我使用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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM