首页 > 解决方案 > Can I duplicate a Layout changing inside Strings?

问题描述

I'm triying to figure out how to duplicate a layout with information as many records I have.

In this App I want as many comments I have in the same boxes like the default comment, in this question i will make a simple for, to try it. enter image description here

Here's how I have the layour distribution:

Image

for (int i = 0; i < 3; i++) {
     LinearLayout linlay = new LinearLayout(getApplicationContext()); //ComentarioEjemplo
     LinearLayout head = new LinearLayout(getApplicationContext()); //LinearLayout
     TextView num_com = new TextView(getApplicationContext()); //TextView
     TextView admin = new TextView(getApplicationContext()); //TextView5
     View separ = new View(getApplicationContext()); // Separador
     TextView coment = new TextView(getApplicationContext()); // TextView6



                    comentarios_layout.addView(linlay);

                    num_com.setText("com");
                    admin.setText("adn");
                    coment.setText("tasuhdfsadh kasjdh as");

                    // STYLE ///////////////////////////////////////////////
                    linlay.setBackgroundColor(Color.parseColor("#d2d2d2"));
                    linlay.setOrientation(LinearLayout.VERTICAL);
                    head.setOrientation(LinearLayout.HORIZONTAL);
                    LinearLayout.LayoutParams layoutLinlay = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

                    LinearLayout.LayoutParams layoutHead = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
                    layoutLinlay.setMargins(20, 5, 20, 5);

                    linlay.setLayoutParams(layoutLinlay);
                    head.setLayoutParams(layoutHead);

                    linlay.addView(head);
                    linlay.addView(separ);
                    linlay.addView(coment);

                    head.addView(num_com);
                    head.addView(admin);
}

I tried to change the style programming but im really struggling with it. Is there any simple way to do this??

标签: androidlayoutstyles

解决方案


您可以使用回收器视图轻松实现此目的,您必须在 xml 中设计一次,并且可以将该布局重复用于多行,查看此链接以获得更详细的答案https://code.tutsplus.com/tutorials/开始使用-recyclerview-and-cardview-on-android--cms-23465


推荐阅读