CERCA
PER MODELLO
FullScreen Chatbox! :)

Utente del giorno: megthebest con ben 1 Thanks ricevuti nelle ultime 24 ore
Utente della settimana: carotix con ben 5 Thanks ricevuti negli ultimi sette giorni
Utente del mese: carotix con ben 19 Thanks ricevuti nell'ultimo mese

Visualizzazione dei risultati da 1 a 1 su 1
Discussione:

Allineamento con Relative Layout

Se questa discussione ti è stata utile, ti preghiamo di lasciare un messaggio di feedback in modo che possa essere preziosa in futuro anche per altri utenti come te!
  1. #1
    Baby Droid


    Registrato dal
    Mar 2011
    Messaggi
    2

    Ringraziamenti
    0
    Ringraziato 0 volte in 0 Posts
    Predefinito

    Allineamento con Relative Layout

    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
    2
    if(cur.moveToFirst()){  
    3
                 int j=0;
    4
                for (  j=0;j<nLuci;j++){
    5
                    t2[j] =new TextView(this);
    6
    7
                    b1[j] = new Button(this);
    8
                    b2[j] = new Button(this);
    9
                    //Button b1 = new Button(this);
    10
                    //TextView t2 = new TextView(this);
    11
                    t2[j].append(""+Integer.parseInt(cur.getString(indiceColonne)));
    12
                    t2[j].append(" "+cur.getString(indiceColonne2));
    13
                    //Button b2 =  new Button(this);
    14
                    b1[j].setText("b1 "+j);
    15
                    b2[j].setText("b2 "+j);
    16
                    
    17
                    t2[j].setId(j);
    18
                    b1[j].setId(j+nLuci+1);
    19
                    b2[j].setId(j+2*nLuci+1);
    20
                   // t2[].setId(j);
    21
                    //b1.setId(j+nLuci+1);
    22
                    //b2.setId(j+2*nLuci+1);
    23
                    Log.i(null, "ID t2_"+j+" "+t2[j].getId());
    24
                    Log.i(null, "ID b1_"+j+" "+b1[j].getId());
    25
                    Log.i(null, "ID b2_"+j+" "+b2[j].getId());
    26
                    
    27
                    
    28
                    RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
    29
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    30
                    if(j==0){
    31
                        lp1.addRule(RelativeLayout.RIGHT_OF, t2[j].getId());
    32
                        Log.i(null, "b1_0: RIGHT"+t2[j].getId());
    33
                    }
    34
                    if(j>0){
    35
                        Log.i(null, "b1_1: BELOW"+b1[j-1].getId());
    36
                        lp1.addRule(RelativeLayout.BELOW, b1[j-1].getId() );
    37
                    }
    38
                        
    39
                    contenitore.addView(b1[j], lp1);
    40
                    
    41
                    RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
    42
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    43
                    if(j==0){
    44
                        Log.i(null, "b2_0: RIGHT "+b1[j].getId());
    45
                        lp2.addRule(RelativeLayout.RIGHT_OF, b1[j].getId());
    46
                    }
    47
                    if(j>0){
    48
                        Log.i(null, "b2_0: RIGHT "+b1[j].getId());
    49
                        lp2.addRule(RelativeLayout.RIGHT_OF, b1[j].getId());
    50
                        Log.i(null, "b2_1: BELOW "+b1[j].getId());
    51
                        lp2.addRule(RelativeLayout.BELOW, b2[j-1].getId());
    52
                    }
    53
                            
    54
                    contenitore.addView(b2[j], lp2);
    55
                   /* contenitore.addView(b1,ViewGroup.LayoutParams.WRAP_CONTENT);
    56
                    contenitore.addView(t2,50,30);
    57
                    contenitore.addView(b2,80,30);*/
    58
                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
    59
                            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    60
                            if(j==0){
    61
                                lp.addRule(RelativeLayout.RIGHT_OF, b2[j].getId());
    62
                                Log.i(null, "t1_0: LEFT "+t2[j].getId());
    63
                            }
    64
                            if(j>0){
    65
                                lp.addRule(RelativeLayout.RIGHT_OF, b2[j].getId());
    66
                                Log.i(null, "t1_1: BELOW "+t2[j-1].getId());
    67
                            }
    68
                            contenitore.addView(t2[j],lp);
    69
                    cur.moveToNext();
    70
             }
    71
            }
    72


    Mentre questo è la parte di foglio xml a cui faccio riferimento:
    8
     
    1
    2
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3
          android:orientation="vertical"
    4
          android:layout_width="fill_parent"
    5
          android:layout_height="fill_parent"
    6
          android:id="@+id/layout2">
    7
        </RelativeLayout>
    8


    Qualcuno può aiutarmi?

  2.  

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire risposte
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Torna su
Privacy Policy