Salve a tutti, come secondo post volevo chiedere se qualcuno poteva aiutarmi a risolvere questo problema di disposizione degli oggetti in un layout in Android.
In particolare vorrei creare da codice un insieme di due bottoni e una textView e disporli nell'ordine Bottone1 Bottone2 TextView1. Essendo dentro un for, ogni riga deve essere allineata con quella sopra.
Sono riuscito con un RelativeLayout ad allineare i bottoni, ma ho problemi nelle textView che mi appaiono una sopra l'altra generando una stringa incomprensibile. Eppure ho utilizzato gli stessi LayoutParameter come regole...
Questo è il mio codice:
1
2if(cur.moveToFirst()){
3int j=0;
4for ( j=0;j<nLuci;j++){
5t2[j] =new TextView(this);
6
7b1[j] = new Button(this);
8b2[j] = new Button(this);
9//Button b1 = new Button(this);
10//TextView t2 = new TextView(this);
11t2[j].append(""+Integer.parseInt(cur.getString(indiceColonne)));
12t2[j].append(" "+cur.getString(indiceColonne2));
13//Button b2 = new Button(this);
14b1[j].setText("b1 "+j);
15b2[j].setText("b2 "+j);
16
17t2[j].setId(j);
18b1[j].setId(j+nLuci+1);
19b2[j].setId(j+2*nLuci+1);
20// t2[].setId(j);
21//b1.setId(j+nLuci+1);
22//b2.setId(j+2*nLuci+1);
23Log.i(null, "ID t2_"+j+" "+t2[j].getId());
24Log.i(null, "ID b1_"+j+" "+b1[j].getId());
25Log.i(null, "ID b2_"+j+" "+b2[j].getId());
26
27
28RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
29RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
30if(j==0){
31lp1.addRule(RelativeLayout.RIGHT_OF, t2[j].getId());
32Log.i(null, "b1_0: RIGHT"+t2[j].getId());
33}
34if(j>0){
35Log.i(null, "b1_1: BELOW"+b1[j-1].getId());
36lp1.addRule(RelativeLayout.BELOW, b1[j-1].getId() );
37}
38
39contenitore.addView(b1[j], lp1);
40
41RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
42RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
43if(j==0){
44Log.i(null, "b2_0: RIGHT "+b1[j].getId());
45lp2.addRule(RelativeLayout.RIGHT_OF, b1[j].getId());
46}
47if(j>0){
48Log.i(null, "b2_0: RIGHT "+b1[j].getId());
49lp2.addRule(RelativeLayout.RIGHT_OF, b1[j].getId());
50Log.i(null, "b2_1: BELOW "+b1[j].getId());
51lp2.addRule(RelativeLayout.BELOW, b2[j-1].getId());
52}
53
54contenitore.addView(b2[j], lp2);
55/* contenitore.addView(b1,ViewGroup.LayoutParams.WRAP_CONTENT);
56contenitore.addView(t2,50,30);
57contenitore.addView(b2,80,30);*/
58RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
59RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
60if(j==0){
61lp.addRule(RelativeLayout.RIGHT_OF, b2[j].getId());
62Log.i(null, "t1_0: LEFT "+t2[j].getId());
63}
64if(j>0){
65lp.addRule(RelativeLayout.RIGHT_OF, b2[j].getId());
66Log.i(null, "t1_1: BELOW "+t2[j-1].getId());
67}
68contenitore.addView(t2[j],lp);
69cur.moveToNext();
70}
71}
72
Mentre questo è la parte di foglio xml a cui faccio riferimento:
81
2<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
3android:orientation="vertical"
4android:layout_width="fill_parent"
5android:layout_height="fill_parent"
6android:id="@+id/layout2">
7</RelativeLayout>
8
Qualcuno può aiutarmi?