Je suis assez près d'aller pour une alternative LinearLayout, mais son genre d'irritant ne pas obtenir ce droit. Pour obtenir le maximum de flexibilité, j'ai défini un fichier XML TableLayout avec uniquement un en-tête de ligne défini. Ensuite, j'ai généré un xml TableRow distinct définissant le "modèle de ligne". En Javacode j'ai sous-classé TableRow et dans le constructeur je gonfle le template tablerow pour attacher à la racine (la classe sous-classée).Layouttroubles étendant tablerow et gonflant xer tablerow
Eh bien, bien jusqu'à présent. Lorsque la table est remplie, le headerrow est correct, mais les autres lignes ne le sont pas. On dirait qu'ils sont disposés différemment, les deux colonnes ne remplissent pas toute la largeur comme prévu et les colonnes ne sont donc pas alignées correctement l'une par rapport à l'autre.
Quelqu'un qui peut faire la lumière sur celui-ci? J'ai essayé beaucoup de solutions, mais rien ne le fait fonctionner.
Le TableLayout avec en-tête ligne
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true" >
<TableLayout
android:id="@+id/zone_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_horizontal"
android:clipToPadding="false" >
<TextView
android:layout_width="0dip"
android:layout_weight="0.8"
android:background="#ffcccccc"
android:text="Zonename"
android:textColor="@android:color/black" />
<TextView
android:layout_width="0dip"
android:layout_weight="0.2"
android:background="#ffcccc00"
android:gravity="right"
android:text="Antall"
android:textColor="@android:color/black" />
</TableRow>
</TableLayout>
</HorizontalScrollView>
</ScrollView>
La "autre" rangée gonflé
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/zonetablerow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/zonerow_name"
android:layout_width="0dip"
android:layout_weight="0.8"
android:background="#ffcccccc"
android:textSize="18dp" />
<TextView
android:id="@+id/zonerow_invcount"
android:layout_width="0dip"
android:layout_gravity="right"
android:layout_weight="0.2"
android:background="#ffcccc00"
android:textSize="18dp" />
</TableRow>
Classe extension TableRow
public class ZoneRow extends TableRow {
private ZoneInventoryDAO dao = null;
private int inventoryCount = 0;
public ZoneRow(Context ctx, ZoneInventoryDAO dao) {
this(ctx, dao, 0);
}
public ZoneRow(Context ctx, ZoneInventoryDAO dao, int inventoryCount) {
super(ctx);
setWeightSum(1.0f);
this.dao = dao;
this.inventoryCount = inventoryCount;
doLayout();
}
private void doLayout() {
// XML layouten settes med zonerow som parent (se:
// http://developer.android.com/resources/articles/layout-tricks-merge.html)
View v = LayoutInflater.from(getContext()).inflate(R.layout.zonerow,
this, true);
TextView t = (TextView) findViewById(R.id.zonerow_name);
TextView cnt = (TextView) findViewById(R.id.zonerow_invcount);
t.setText(dao.getZoneAlias());
cnt.setText(String.valueOf(inventoryCount));
}
public void incInventory() {
inventoryCount++;
}
public ZoneInventoryDAO getDAO() {
return dao;
}
}