我有了下一个布局:
<?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>
我想以编程方式将这个布局设置为TableRow。任何想法?
谢谢。
正如christian所建议的,使用LayoutInflater解决了我的问题。这是我的活动的代码:
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);
}
我用我想插入到每一行中的布局"膨胀"一个视图,并且我在布局中获取组件,操作它们,然后将视图插入到行中。最后,我只需要将行插入到表中,就这样了!