开发者

Set LinearLayout into TableRow programmatically

I have the next开发者_JS百科 layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/foto"
        android:scaleType="fitCenter"
        android:layout_width="70dp"
        android:layout_height="70dp"/>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:paddingLeft="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="fill_parent"
            android:id="@+id/nombre"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:textColor="#ffffff"
            android:textSize="16sp" />
        <TextView
            android:layout_width="fill_parent"
            android:id="@+id/posicion"
            android:textColor="#ffffff"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

And I would like to set this layout to a TableRow programmatically. Any idea?

Thanks.


As Cristian suggested, using LayoutInflater solved my problem. This is the code for my activity:

public class SeleccionarJugador extends Activity {

private TableLayout mTablaJugadores;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    // Se establece el layout
    setContentView(R.layout.jugadores);

    // Se recupera la tabla donde se insertán los jugadores de ambos equipos
    mTablaJugadores = (TableLayout) findViewById(R.id.tabla_jugadores);
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    TableRow fila = new TableRow(this);
    View v = inflater.inflate(R.layout.fila_seleccionar_jugadores,null);
    TextView dorsal = (TextView) v.findViewById(R.id.dorsal);
    dorsal.setText(String.valueOf(11));
    TextView nombre = (TextView) v.findViewById(R.id.nombre);
    nombre.setText("Pepe");
    TextView posicion = (TextView) v.findViewById(R.id.posicion);
    posicion.setText("Lateral");
    fila.addView(v);
    mTablaJugadores.addView(fila);

}

I "inflate" one View with the layout which I would like to insert into each row, and I take the components inside the Layout, manipulate them, and then insert the View into the row. Finally, just I have to insert the row into the Table and That's it!.

Thanks a lot.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜