Android - Dynamically Add Views into View

后端 未结 5 2006
情书的邮戳
情书的邮戳 2020-11-22 03:58

I have a layout for a view -



        
5条回答
  •  感情败类
    2020-11-22 04:36

    // Parent layout
    LinearLayout parentLayout = (LinearLayout)findViewById(R.id.layout);
    
    // Layout inflater
    LayoutInflater layoutInflater = getLayoutInflater();
    View view;
    
    for (int i = 1; i < 101; i++){
        // Add the text layout to the parent layout
        view = layoutInflater.inflate(R.layout.text_layout, parentLayout, false);
    
        // In order to get the view we have to use the new view with text_layout in it
        TextView textView = (TextView)view.findViewById(R.id.text);
        textView.setText("Row " + i);
    
        // Add the text view to the parent layout
        parentLayout.addView(textView);
    }
    

提交回复
热议问题