2011-07-05 22 views
10

Estoy intentando utilizar el diseño dinámico para la parte de comentario de mi proyecto, pero cuando configuro el texto de la vista de texto de forma dinámica, la salida solo aparece en la parte superior de la pantalla. Y pone la salida sobre las otras salidasDynamic TextView en diseño relativo

RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl); 
     for(int i = 0; i < 20; i++) { 
     TextView cb = new TextView(this); 
     cb.setText("YORUMLAR"+yorum[0]+i); 

     cb.setTextSize(30); 
      ll.addView(cb); 

     } 

Entonces, ¿cómo puedo poner la salida en la parte inferior de la pantalla linealmente.

+1

... estableciendo los atributos relativos adecuados para sus textViews; por ahora, solo está configurando el texto, sin posición, y por defecto está en la parte superior de la pantalla. – Adinia

Respuesta

21

Debe usar LinearLayout para agregar automáticamente uno TextView después de otro.


Suponiendo que no puede vivir sin RelativeLayout, necesitará para generar dinámicamente los ID de todas TextView se crea con el fin de poner un punto de vista bajo otro. Aquí es ejemplo:

public class HelloWorld extends Activity 
{  
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    {  
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity); 

     RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout); 

     Random rnd = new Random(); 
     int prevTextViewId = 0;  
     for(int i = 0; i < 10; i++) 
     {      
      final TextView textView = new TextView(this); 
      textView.setText("Text "+i);  
      textView.setTextColor(rnd.nextInt() | 0xff000000);    

      int curTextViewId = prevTextViewId + 1; 
      textView.setId(curTextViewId); 
      final RelativeLayout.LayoutParams params = 
       new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, 
               RelativeLayout.LayoutParams.WRAP_CONTENT); 

      params.addRule(RelativeLayout.BELOW, prevTextViewId); 
      textView.setLayoutParams(params); 

      prevTextViewId = curTextViewId; 
      layout.addView(textView, params); 
     }    
    }  
} 

enter image description here

+0

¡Gracias a un millón! Tengo un cursor que puede tener valores nulos. La gente odiaba ver las cosas en blanco, así que creé una vista de texto dinámica con las instrucciones "if not null, do this" y luego usé tu ejemplo para crear las ID de textview dinámicas para cada una de las que se deben ver. La clave era establecer el curTextViewId = prevTextViewID + 1 en cada instrucción if, al final de cada uno, asegúrese de establecer prevTextViewId = curTextViewId para que la siguiente instrucción if obtenga el nuevo prevTextViewId. Gracias! – Opy

2

Tienes que proporcione la ubicación de la vista que acaba de agregar. Como dijo @Adinia, sin posición, se alineará con la parte superior de forma predeterminada. Entonces puede usar el siguiente código para hacerlo con RelativeLayout;

RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.container); 

for (int i = 0; i < 20; i++) { 

    TextView dynaText = new TextView(this); 

    dynaText.setText("Some text " + i); 
    dynaText.setTextSize(30); 

    // Set the location of your textView. 
    dynaText.setPadding(0, (i * 30), 0, 0); 

    containerLayout.addView(dynaText); 
} 

Si desea mostrar varias textviews una después de la otra, entonces debe ir con LinearLayout.

1

También puede agregar una vista de texto dinámica a la disposición relativa. Aquí con un código adjunto, esto puede ser útil.

RelativeLayout ll=(RelativeLayout) findViewById(R.id.rl); 
    for(int i = 0; i < 20; i++) 
    { 
     TextView cb = new TextView(this); 
     cb.setText("YORUMLAR"+yorum[0]+i); 
     cb.setTextSize(30); 
     cb.setId(2000+i); 
     RelativeLayout.LayoutParams TextViewLayoutParams = new RelativeLayout.LayoutParams(100,100); 
     if (i != 0)DispViewLayoutParams.addRule(RelativeLayout.BELOW, 2000 - (i-1)); 
     ll.addView(cb,TextViewLayoutParams); 
    } 
Cuestiones relacionadas