Pregunta

He estado buscando en todas partes una respuesta a esta pregunta. Soy nuevo en Android e intento agregar elementos a un diseño relativo programáticamente a través de Java en lugar de XML. He creado una clase de prueba para probarlo, pero los elementos siguen apilando en lugar de formatear correctamente. Simplemente quiero una vista de texto debajo del otro por ahora (eventualmente usaré la izquierda y la derecha de los parámetros, pero estoy comenzando simple. ¿Qué me estoy perdiendo?

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);};
¿Fue útil?

Solución

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

Esta línea significa que la parte inferior de TV2 está alineada con el fondo de la televisión, en otras palabras, se cubrirán mutuamente. La propiedad que desea es presumiblemente RelativeLayout.BELOW . Sin embargo, recomiendo usar XML para esto.

Otros consejos

Usar:

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

Le faltan varias cosas, primero el ScrollView no tiene medidas, configúrela con LayoutParams.fill_parent o wrap_content, segundo; It TextView1 está fuera de lugar, por lo que textView2, establece la posición TextView1 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);};
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top