1
我嘗試從代碼創建佈局時出現問題。問題是我正在創建水平和垂直的不同佈局,並且具有權重。我們的想法是創建一個類似於圖像1的顯示器,但現在正在顯示圖像2.以編程方式創建佈局
¿任何人都可以幫我嗎?如果你需要更多的解釋,我會嘗試回答你的問題。
Thx 4 all。
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);
你是了不起的......瘋了2天,這問題讓我...我認爲的LinearLayout方向的默認是垂直的...這是什麼原因由於我以爲它... – kartGIS
謝謝,是的,它默認情況下是HORIZONTAL。所以,你可以刪除所有'setOrientation(LinearLayout.HORIZONTAL)'來簡化代碼。祝你好運。 –