android 以编程方式创建布局

rbl8hiat  于 2023-05-05  发布在  Android
关注(0)|答案(1)|浏览(151)

当我尝试从代码创建布局时遇到了问题。问题是我用weightsum创建了不同的水平和垂直布局。想法是创建类似于图像1的显示,但是现在显示图像2。
Image 1
Image 2
有人能帮我吗如果你需要更多的解释,我会尽量回答你的问题。
Thx 4所有

for (NotificacionClase notifClase Indiv:lClases){

            LinearLayout linNuevo= new LinearLayout(this);
            linNuevo.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams LLParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            linNuevo.setWeightSum(2);
            linNuevo.setLayoutParams(LLParams);

            LinearLayout linNuevoCabecera= new LinearLayout(this);
            linNuevoCabecera.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams llParamCabecera = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1);
            linNuevoCabecera.setWeightSum(2);
            linNuevoCabecera.setLayoutParams(llParamCabecera);
            TextView textFecha = new TextView(this);
            textFecha.setText(notifClaseIndiv.getFecha());
            textFecha.setLayoutParams( new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT,1.4f));

            TextView textTiempoTotal=new TextView(this);
            textTiempoTotal.setText(notifClaseIndiv.getTotal()+ " min.");
            textTiempoTotal.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.5f));
            textTiempoTotal.setTextSize(14);
            textFecha.setTextSize(20);
            linNuevoCabecera.addView(textFecha);
            linNuevoCabecera.addView(textTiempoTotal);

            final ImageView imagFlecha= new ImageView(this);
            imagFlecha.setBackgroundResource(R.drawable.flecha_bajo);
            imagFlecha.setLayoutParams( new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.1f));
            linNuevoCabecera.addView(imagFlecha);

            linNuevo.addView(linNuevoCabecera);
            for(NotificacionAsignaturas clase : notifClaseIndiv.getListClaseObj()){

                LinearLayout linNuevoClaseCabecera= new LinearLayout(this);
                linNuevoClaseCabecera.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1));
                linNuevoClaseCabecera.setOrientation(LinearLayout.VERTICAL);
                linNuevoClaseCabecera.setWeightSum(2);

                LinearLayout linNuevoClase= new LinearLayout(this);
               LinearLayout.LayoutParams linLayParamsNuevoClase=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1);
                linLayParamsNuevoClase.setMargins(30, 0, 0, 0);
                linNuevoClase.setLayoutParams( linLayParamsNuevoClase);
                linNuevoClase.setWeightSum(2);
                linNuevoClase.setOrientation(LinearLayout.HORIZONTAL);

                TextView txtClase = new TextView(this);
                txtClase.setText(clase.getClase());
                txtClase.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1.4f));
                txtClase.setTextSize(18);

                TextView textTiempoClaseTotal=new TextView(this);
                textTiempoClaseTotal.setText(clase.getTotal()+ " min.");
                textTiempoClaseTotal.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,0.5f));
                textTiempoClaseTotal.setTextSize(14);

                linNuevoClase.addView(txtClase);
                linNuevoClase.addView(textTiempoClaseTotal);
                final ImageView imagFlechaClase= new ImageView(this);
                imagFlechaClase.setBackgroundResource(R.drawable.flecha_simple_bajo);
                imagFlechaClase.setLayoutParams( new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT,0.1f));
                linNuevoClase.addView(imagFlechaClase);

                linNuevoClaseCabecera.addView(linNuevoClase);

                 LinearLayout linLayGroupTareas=new LinearLayout(this);
                LinearLayout.LayoutParams linLayParamsGroupTarea=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);
                linLayGroupTareas.setLayoutParams(linLayParamsGroupTarea);
                for(NotificacionTareas tareaIndv: clase.getListTareas()){
                    LinearLayout linNuevaTarea=new LinearLayout(this);
                    LinearLayout.LayoutParams linLayParamsNuevaTarea=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    linLayParamsNuevaTarea.setMargins(45, 0, 0, 0);
                    linNuevaTarea.setLayoutParams( linLayParamsNuevaTarea);
                    linNuevaTarea.setWeightSum(3);
                    linNuevaTarea.setOrientation(LinearLayout.HORIZONTAL);

                    LinearLayout linGroupTarea=new LinearLayout(this);
                    linGroupTarea.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,2.3f));
                    linGroupTarea.setWeightSum(2);
                    linGroupTarea.setOrientation(LinearLayout.VERTICAL);

                    TextView txtAsign= new TextView(this);
                    txtAsign.setText(tareaIndv.getAsignatura());
                    txtAsign.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0,1f));

                    linGroupTarea.addView(txtAsign);
                    TextView txtDescrip= new TextView(this);
                    txtDescrip.setText(tareaIndv.getDescripcion().length()>20?tareaIndv.getDescripcion().substring(0,20)+"...":tareaIndv.getDescripcion());

                    LinearLayout.LayoutParams linLayParamsDescripcion=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1);
                    linLayParamsDescripcion.setMargins(15, 0, 0, 0);
                    txtDescrip.setLayoutParams(linLayParamsDescripcion);

                    linGroupTarea.addView(txtDescrip);

                    TextView txtTiempo= new TextView(this);
                    txtTiempo.setText(tareaIndv.getTiempo() + " min.");
                    txtTiempo.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,0.7f));

                    linNuevaTarea.addView(linGroupTarea);
                    linNuevaTarea.addView(txtTiempo);
                    linLayGroupTareas.addView(linNuevaTarea);


                }
                listTarea.add(linNuevoClaseCabecera);
                linNuevoClaseCabecera.addView(linLayGroupTareas);

                linNuevo.addView(linNuevoClaseCabecera);
                linNuevo.setClickable(true);
                listClasesTarea.add(linNuevo);
            }
            ((LinearLayout)findViewById(R.id.lnLayWindowNotification)).addView(linNuevo);
        }

更具体地说,问题是clase.getListaTareas()有多个元素时。则显示器显示的唯一子项是最后添加的。我认为问题出在这部分代码中,但我不知道…

for(NotificacionTareas tareaIndv: clase.getListTareas()){
                    LinearLayout linNuevaTarea=new LinearLayout(this);
                    LinearLayout.LayoutParams linLayParamsNuevaTarea=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    linLayParamsNuevaTarea.setMargins(45, 0, 0, 0);
                    linNuevaTarea.setLayoutParams( linLayParamsNuevaTarea);
                    linNuevaTarea.setWeightSum(3);
                    linNuevaTarea.setOrientation(LinearLayout.HORIZONTAL);

                    LinearLayout linGroupTarea=new LinearLayout(this);
                    linGroupTarea.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,2.3f));
                    linGroupTarea.setWeightSum(2);
                    linGroupTarea.setOrientation(LinearLayout.VERTICAL);

                    TextView txtAsign= new TextView(this);
                    txtAsign.setText(tareaIndv.getAsignatura());
                    txtAsign.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0,1f));

                    linGroupTarea.addView(txtAsign);
                    TextView txtDescrip= new TextView(this);
                    txtDescrip.setText(tareaIndv.getDescripcion().length()>20?tareaIndv.getDescripcion().substring(0,20)+"...":tareaIndv.getDescripcion());

                    LinearLayout.LayoutParams linLayParamsDescripcion=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1);
                    linLayParamsDescripcion.setMargins(15, 0, 0, 0);
                    txtDescrip.setLayoutParams(linLayParamsDescripcion);

                    linGroupTarea.addView(txtDescrip);

                    TextView txtTiempo= new TextView(this);
                    txtTiempo.setText(tareaIndv.getTiempo() + " min.");
                    txtTiempo.setLayoutParams( new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,0.7f));

                    linNuevaTarea.addView(linGroupTarea);
                    linNuevaTarea.addView(txtTiempo);
                    linLayGroupTareas.addView(linNuevaTarea);


                }
                listTarea.add(linNuevoClaseCabecera);
                linNuevoClaseCabecera.addView(linLayGroupTareas);

                linNuevo.addView(linNuevoClaseCabecera);
ctehm74n

ctehm74n1#

我猜 linLayGroupTareas 应该是垂直
在循环之前添加这一行:

linLayGroupTareas.setOrientation(LinearLayout.VERTICAL);

相关问题