sergiodebst
30/10/11, 22:48:19
Buenas, ahora en el cole estoy empezando a tocar cosillas de android y a mi como que me ha tocado la fibra y me ha dado por curiosear por mi cuenta y ir probando witgets y cosillas, en fin al lio xD
Vereis lo que quiero conseguir es una interfaz como esta:
http://img442.imageshack.us/img442/1714/pantallazo1gg.png
Esto lo consigo facil haciendo a mano el XML con este codigo:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="366dp" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<Button
android:id="@+id/btn1"
android:layout_weight="1"
android:contentDescription="2130968577"
android:onClick="iniciar" />
<Button
android:id="@+id/btn2"
android:layout_weight="1"
android:contentDescription="2130968576"
android:onClick="iniciar" />
<Button
android:id="@+id/btn3"
android:layout_weight="1"
android:contentDescription="2130968578"
android:onClick="iniciar" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_marginTop="20dp" android:onClick="stop" android:layout_marginLeft="125dp"/>
</LinearLayout>
El problema sucede cuando he de meter un numero de botones que supongamos que yo no se o es muy grande de manera que no puedo hacer a mano el XML, entonces me interesa hacer la tabla con codigo, indagando un poco por google he conseguido sacar esto:
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
//Pillo la tabla del XML
TableLayout tabla = (TableLayout)findViewById(R.id.tableLayout1);
//Creo una nueva fila
TableRow tr = new TableRow(this);
//Creo los parametros que debe de tener la fila y se los asigno
LayoutParams parametrosRow= new LayoutParams(LayoutParams.WRAP_CONTENT);
parametrosRow.setMargins(2, 2, 2, 0);
tr.setLayoutParams(parametrosRow);
//Creo un boton
Button b = new Button(this);
b.setText("Button");
//meto el boton en la fila
tr.addView(b);
//Repito el proceso 2 veces mas para tener 3 botones
b = new Button(this);
b.setText("Button2");
tr.addView(b);
b = new Button(this);
b.setText("Button3");
tr.addView(b);
//Lo meto todo en el tableLayout
tabla.addView(tr,parametrosRow);
}
NOTA: En parametrosRow he puesto 2 porque mas o menos me imagino que 10dp son unos 2 px
Codigo XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="366dp" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_marginTop="20dp" android:onClick="stop" android:layout_marginLeft="125dp"/>
</LinearLayout>
El problema es que con esto me queda asi:
http://img38.imageshack.us/img38/631/pantallazo2jk.png
No sé donde estoy metiendo la gamba, a ver si alguien con mas experiencia creando este tipo de cosas desde codigo me puede dar un poco de orientacion.
Gracias de antemano.
PD: Ya se que con esto no automatizo nada, pero prefiero saber hacerlo statico por codigo y hacerlo bien antes de meterme en bucles.
PD2: Si a alguien le interesa, lo del codigo lo he sacado de http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout
Vereis lo que quiero conseguir es una interfaz como esta:
http://img442.imageshack.us/img442/1714/pantallazo1gg.png
Esto lo consigo facil haciendo a mano el XML con este codigo:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="366dp" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<Button
android:id="@+id/btn1"
android:layout_weight="1"
android:contentDescription="2130968577"
android:onClick="iniciar" />
<Button
android:id="@+id/btn2"
android:layout_weight="1"
android:contentDescription="2130968576"
android:onClick="iniciar" />
<Button
android:id="@+id/btn3"
android:layout_weight="1"
android:contentDescription="2130968578"
android:onClick="iniciar" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_marginTop="20dp" android:onClick="stop" android:layout_marginLeft="125dp"/>
</LinearLayout>
El problema sucede cuando he de meter un numero de botones que supongamos que yo no se o es muy grande de manera que no puedo hacer a mano el XML, entonces me interesa hacer la tabla con codigo, indagando un poco por google he conseguido sacar esto:
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
//Pillo la tabla del XML
TableLayout tabla = (TableLayout)findViewById(R.id.tableLayout1);
//Creo una nueva fila
TableRow tr = new TableRow(this);
//Creo los parametros que debe de tener la fila y se los asigno
LayoutParams parametrosRow= new LayoutParams(LayoutParams.WRAP_CONTENT);
parametrosRow.setMargins(2, 2, 2, 0);
tr.setLayoutParams(parametrosRow);
//Creo un boton
Button b = new Button(this);
b.setText("Button");
//meto el boton en la fila
tr.addView(b);
//Repito el proceso 2 veces mas para tener 3 botones
b = new Button(this);
b.setText("Button2");
tr.addView(b);
b = new Button(this);
b.setText("Button3");
tr.addView(b);
//Lo meto todo en el tableLayout
tabla.addView(tr,parametrosRow);
}
NOTA: En parametrosRow he puesto 2 porque mas o menos me imagino que 10dp son unos 2 px
Codigo XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="366dp" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</ScrollView>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:layout_marginTop="20dp" android:onClick="stop" android:layout_marginLeft="125dp"/>
</LinearLayout>
El problema es que con esto me queda asi:
http://img38.imageshack.us/img38/631/pantallazo2jk.png
No sé donde estoy metiendo la gamba, a ver si alguien con mas experiencia creando este tipo de cosas desde codigo me puede dar un poco de orientacion.
Gracias de antemano.
PD: Ya se que con esto no automatizo nada, pero prefiero saber hacerlo statico por codigo y hacerlo bien antes de meterme en bucles.
PD2: Si a alguien le interesa, lo del codigo lo he sacado de http://en.androidwiki.com/wiki/Dynamically_adding_rows_to_TableLayout