2011-01-31 11 views
16

Ho cercato ovunque una risposta a questa domanda. Sono nuovo di Android e sto tentando di aggiungere elementi a un layout relativo a livello di codice tramite java anziché xml. Ho creato una classe di test per provarla ma gli elementi continuano a impilare invece di formattare correttamente. Voglio semplicemente uno TextView sotto l'altro per ora (alla fine userò sinistra ea destra dei parametri, ma sto iniziando semplice. Quello che mi manca?Aggiunta di elementi a un layout relativo a livello di programmazione

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ScrollView sv = new ScrollView(this); 
    RelativeLayout ll = new RelativeLayout(this); 
    ll.setId(99); 
    sv.addView(ll); 
    TextView tv = new TextView(this); 
    tv.setText("txt1"); 
    tv.setId(1); 
    TextView tv2 = new TextView(this); 
    tv2.setText("txt2"); 
    tv2.setId(2); 
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    ll.addView(tv, lay); 
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId()); 
    ll.addView(tv2, p); this.setContentView(sv);}; 

risposta

20
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId()); 

Questa linea significa che il fondo di tv2 è allineato con il fondo della tv - in altre parole, si copriranno l'un l'altro.La proprietà che si desidera è presumibilmente RelativeLayout.BELOW. Tuttavia, ti consiglio vivamente di utilizzare invece xml per questo invece

+1

Grazie, era in realtà RelativeLayout.Below, ma abbastanza vicino. Il problema con il fare xml alla fine sarà popolato da un database esterno. – user597436

2

ti mancano varie cose, prima ScrollView non ha misure, impostalo con LayoutParams.FILL_PARENT o WRAP_CONTENT, in secondo luogo, TextView1 non è posizionato correttamente TextView2, s et TextView1 posizione con lay.addRule (RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ScrollView sv = new ScrollView(this); 
    RelativeLayout ll = new RelativeLayout(this); 
    ll.setId(99); 

    sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    TextView tv = new TextView(this); 
    tv.setText("txt1"); 
    tv.setId(1); 

    TextView tv2 = new TextView(this); 
    tv2.setText("txt2"); 
    tv2.setId(2); 
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lay.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
    ll.addView(tv, lay); 
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId()); 
    ll.addView(tv2, p); 
this.setContentView(sv);}; 
+0

c'è un errore qui, la riga p.addRule (RelativeLayout.ALIGN_BOTTOM, tv.getId()); deve essere proprio come dice @Cristian: p.addRule (RelativeLayout.BELOW, tv.getId()); – Franco

5

Usa:

p.addRule(RelativeLayout.BELOW, tv.getId()); 
Problemi correlati