Ho una matrice di lunghezza n, ora ho bisogno di creare n numero di LinearLayouts e aggiungere cose diverse su ognuno di essi. Come può essere fatto in modo dinamico?Come aggiungere dinamicamente LinearLayout su Android?
5
A
risposta
4
LinearLayout lLayour = new LinearLayout(context);
parentWidget.addView(lLayout);
4
Il modo più semplice è quello di creare un layout in XML e gonfiare utilizzando
LayoutInflater.from(context).inflate(R.layout.my_linear_layout);
Si consiglia inoltre di setId()
vostre opinioni aggiunto in modo da potervi accedere facilmente in seguito.
2
Ho risolto utilizzando RelativeLayout che ho trovato un po 'più facile da lavorare. Sì, certo, come i ragazzi sopra citati, ho usato setId()
. Ecco il codice che ho implementato:
@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);
}
Problemi correlati
- 1. Android come aggiungere colpo gesto LinearLayout senza onDown vero
- 2. creando dinamicamente tablelayout (android)
- 3. Android: LinearLayout addView Animazione
- 4. selettore di sfondo android linearlayout
- 5. Come disabilitare/abilitare tutti i bambini su LinearLayout in Android
- 6. Aggiungere dinamicamente alla fine di RelativeLayout
- 7. Android TableRow - Come aggiungere Visualizza dinamicamente in determinate posizioni?
- 8. Come aggiungere un TextView dinamicamente negli oggetti ListView Android?
- 9. Come aggiungere l'attributo XmlInclude dinamicamente
- 10. Come aggiungere la funzionalità di scorrimento su Android CardView?
- 11. Come aggiungere dinamicamente elementi all'array di stringhe?
- 12. Android LinearLayout fill-the-middle
- 13. Android - Centro Textview in LinearLayout
- 14. Android ImageView dimensionato per peso non centrato su LinearLayout principale
- 15. Android - ClassCastException - su LayoutParams da LinearLayout a AbsListView
- 16. Come aggiungere più TextView dynamicall per definire LinearLayout in main.xml
- 17. LinearLayout con divisori su pre Honeycomb
- 18. Problema con onclicklistener su LinearLayout
- 19. Come aggiungere pulsanti dinamicamente al mio modulo?
- 20. Come aggiungere dinamicamente righe di tabella
- 21. Come aggiungere effetti sonori su Android?
- 22. come aggiungere vitamio dependecny su android studio
- 23. Android Studio come creare un LinearLayout a 2 colonne
- 24. Android: modifica dinamicamente l'immagine in Listview
- 25. Android: strana inversione peso in LinearLayout
- 26. android- Come impostare 2 xml drawable per LinearLayout
- 27. Come aggiungere un bordo colorato su cardview?
- 28. Come aggiungere GLSurfaceView tra le viste Android?
- 29. come aggiungere dinamicamente ancoraggio/href in jQuery
- 30. Come aggiungere dinamicamente intervallo al timestamp?
E da qualche parte in là si assegna tutte le sue proprietà (supponendo che avete bisogno di qualcosa di diverso da default). – jwatts1980
È inoltre possibile impostare LayoutParams per LinearLayout figlio. lLayout.setLayoutPatams (layoutParams), – ihrupin
@ihrupin come ottenere parentWidget? Sono all'interno di un metodo onCreate di una classe che eredita la classe Activity e la vista è setContentView (R.layout.someLayout) –