Tengo una matriz de longitud n, ahora necesito crear n cantidad de LinearLayouts y agregar elementos diferentes en cada uno de ellos. ¿Cómo se puede hacer de forma dinámica?¿Cómo agregar dinámicamente LinearLayout en Android?
5
A
Respuesta
4
LinearLayout lLayour = new LinearLayout(context);
parentWidget.addView(lLayout);
4
La forma más fácil es crear un diseño en XML y inflarlo usando
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
Usted también puede querer setId()
sus puntos de vista Agregado para que pueda acceder a ellos fácilmente más adelante.
2
Lo resolví usando RelativeLayout, que me resultó un poco más fácil de trabajar. Sí, por supuesto, como los chicos señalaron anteriormente, utilicé setId()
. Este es el código que implementé:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
//Parent RelativeLayout
parentLayout = new RelativeLayout(this);
parentLayout.setBackgroundColor(Color.WHITE);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
parentLayout.setLayoutParams(params);
sv.addView(parentLayout);
final String[] comList = getCommunication();
int listLength=0;
try{
listLength= comList.length/3;
}catch(Exception e){System.out.println(e);System.exit(0);}
childLayout= new RelativeLayout[listLength] ;
TextView[] tvName = new TextView[listLength];
TextView[] tvDate =new TextView[listLength];
TextView[] tvMsg =new TextView[listLength];
for(int i =0;i<listLength;i++){
try{
childLayout[i] = new RelativeLayout(this);
childLayout[i].setPadding(5, 5, 5, 5);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
if(i==0){params.addRule(RelativeLayout.BELOW);}
else{params.addRule(RelativeLayout.BELOW,i);}
childLayout[i].setId(i+1);
childLayout[i].setClickable(true);
childLayout[i].setLayoutParams(params);
childLayout[i].setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
//Create the intent
Intent i = new Intent("ACTIIVTY");
startActivity(i);
}
});
tvName[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
tvName[i].setLayoutParams(params);
childLayout[i].addView(tvName[i]);
if(comList[i*3].length()>24){
String name = comList[i*3].substring(0,24)+"...";
tvName[i].setText(name);
}else{
tvName[i].setText(comList[i*3]);
}
tvName[i].setId(listLength+1+i);
tvName[i].setTextSize(12);
tvName[i].setTextColor(Color.BLACK);
tvDate[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
tvDate[i].setLayoutParams(params);
childLayout[i].addView(tvDate[i]);
tvDate[i].setTextSize(11);
tvDate[i].setTextColor(Color.BLUE);
tvDate[i].setText(comList[i*3+1]);
tvMsg[i] = new TextView(this);
params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, listLength+1+i);
tvMsg[i].setLayoutParams(params);
childLayout[i].addView(tvMsg[i]);
tvMsg[i].setTextSize(11);
tvMsg[i].setTextColor(Color.GRAY);
if(comList[i*3+2].length()>96){
String msg = comList[i*3+2].substring(0,96)+"...";
tvMsg[i].setText(msg);
}else{
tvMsg[i].setText(comList[i*3+2]);
}
parentLayout.addView(childLayout[i]);
}catch(Exception e){System.out.println("Errrorrrrr");}
}
setContentView(sv);
}
Cuestiones relacionadas
- 1. Android + setDividerDrawable en LinearLayout?
- 2. ¿Cómo agregar Canvas en un LinearLayout específico?
- 3. Cambia dinámicamente el fondo LinearLayout
- 4. Agregue textViews dinámicamente a linearLayout
- 5. android linearlayout background selector
- 6. Android: ScrollView en LinearLayout vertical
- 7. Android 3 - Agregar un fragmento a LinearLayout: fill_parent no funciona
- 8. Android: LinearLayout addView Animation
- 9. creando dinámicamente tablelayout (android)
- 10. Cómo agregar programáticamente ImageView en LinearLayout sin espacios adicionales?
- 11. Android LinearLayout Gradient Background
- 12. Cómo agregar métodos dinámicamente
- 13. Android LinearLayout fill-the-middle
- 14. Android TableRow - ¿Cómo agregar View dinámicamente a cierta posición?
- 15. ¿Agregar vista a un fragmento dinámicamente en Android?
- 16. Eliminar espacio entre botones en LinearLayout (Android)
- 17. Android LinearLayout llenar el ancho
- 18. Android: cambie dinámicamente la imagen en Listview
- 19. ¿Cómo agregar dinámicamente un attr_reader
- 20. Cómo agregar atributos XmlInclude dinámicamente
- 21. Android - Centro TextView horizontalmente en LinearLayout
- 22. Actualizar un LinearLayout después de agregar una vista
- 23. cómo agregar dinámicamente anclaje/href en jQuery
- 24. asp.net dinámicamente agregar GridViewRow
- 25. layout_gravity en LinearLayout
- 26. ¿Cómo centrar un ImageView en LinearLayout?
- 27. ZooKeeper: ¿agregar compañeros dinámicamente?
- 28. ¿Cómo se puede desplazar LinearLayout?
- 29. Obteniendo error al agregar fragmentos dinámicamente -java.lang.IllegalStateException:
- 30. Relativelayout o LinearLayout en ios iphone development?
Y en algún lugar asignas todas sus propiedades (suponiendo que necesites algo distinto de los valores predeterminados). – jwatts1980
También puede establecer LayoutParams para LinearLayout hijo. lLayout.setLayoutPatams (layoutParams), – ihrupin
@ihrupin ¿cómo obtener parentWidget? Estoy dentro de un método onCreate de una clase que hereda clase de actividad y la vista es setContentView (R.layout.someLayout) –