以编程方式创建布局
Creating layouts programmatically
我尝试从代码创建布局时遇到问题。问题是我正在使用权重和创建水平和垂直的不同布局。这个想法是创建一个类似于图像 1 的显示,但现在显示的是图像 2。
Image 1
Image 2
¿任何人都可以帮助我吗?如果您需要更多解释,我会尽力回答您的问题。
谢谢大家 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() 的元素多于一个。那么显示器显示的唯一child就是最后添加的。我认为问题出在这部分代码中,但我不知道...
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);
我猜 linLayGroupTareas 应该是 VERTICAL.
在循环之前添加这一行:
linLayGroupTareas.setOrientation(LinearLayout.VERTICAL);
我尝试从代码创建布局时遇到问题。问题是我正在使用权重和创建水平和垂直的不同布局。这个想法是创建一个类似于图像 1 的显示,但现在显示的是图像 2。
Image 1
Image 2
¿任何人都可以帮助我吗?如果您需要更多解释,我会尽力回答您的问题。
谢谢大家 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() 的元素多于一个。那么显示器显示的唯一child就是最后添加的。我认为问题出在这部分代码中,但我不知道...
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);
我猜 linLayGroupTareas 应该是 VERTICAL.
在循环之前添加这一行:
linLayGroupTareas.setOrientation(LinearLayout.VERTICAL);