2

私は LinearLayout の代替案にかなり近づいていますが、これを正しく理解していないのはちょっとイライラします。最大限の柔軟性を得るために、行ヘッダーのみが定義された TableLayout xml を定義しました。次に、「行テンプレート」を定義する個別の TableRow xml を生成しました。Javacode で TableRow をサブクラス化し、コンストラクターで tablerow テンプレートを拡張してルート (サブクラス化されたクラス) にアタッチします。

まあ、ここまではいい。テーブルにデータが入力されると、ヘッダー行は問題ありませんが、他の行は問題ありません。それらが異なる方法でレイアウトされているようです.2つの列が期待どおりに幅全体を埋めていないため、列が互いに正しく配置されていません.

これに光を当てることができる人はいますか?私は多くの解決策を試しましたが、何もうまくいきません。

ヘッダー行付きのテーブル レイアウト

<?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>

「その他」の膨らんだ行

    <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>

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;
    }

}
4

3 に答える 3

0

テーブルローを拡張し、そのオブジェクトのインスタンスを作成しているように見えます。これにより、テーブル行が作成されます。次に、別のテーブルローを膨らませています。これは、何らかの理由で最初のテーブルローと相互作用しています。簡単に修正するには、table.setColumnStretchable(0、true);のようなものを追加してみてください。

于 2012-04-19T16:23:02.380 に答える