1

XML で定義されたレイアウトに基づいて、新しいカスタム LinearLayout クラスをインスタンス化できるようにしたいと考えています。私が何をしても、実行時に LinearLayout とすべての TextViews が null 参照例外をスローします。

transferListRow.axml

<?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"
android:layout_margin="5dp"
android:padding="5dp">
<TextView
    android:id="@+id/transferSerial"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/transferModel"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<TextView
    android:id="@+id/transferSite"
    android:layout_width="0dp"
    android:layout_weight=".30"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center" />
<Button
    android:id="@+id/transferDelete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:padding="5dp"
    android:gravity="center"
    android:src="@drawable/delete" />

TransferListRow.cs 試行 1:

 sealed class TransferListRow : LinearLayout
 {
    private readonly Context _context;

    public TransferListRow(Context context, string serial, string model, string site)
        :base(context)
    {
        _context = context;

        LayoutInflator inflator = (LayoutInflator) _context.GetSystemService(Context.LayoutInflaterService);
        inflator.Inflate(Resource.Layout.transferListRow, null);

        TextView s = (TextView) FindViewById(Resource.Id.transferSerial);
        s.Text = serial;

        TextView m = (TextView) FindViewById(Resource.Id.transferModel);
        m.Text = model;

        TextView st = (TextView) FindViewById(Resource.Id.transferSite);
        st.Text = site;
    }
}

TransferListRow.cs 試行 2:

sealed class TransferListRow : LinearLayout
{
    private readonly Context _context;

    public TransferListRow(Context context, string serial, string model, string site)
        :base(context)
    {
        _context = context;

        LinearLayout layout = (LinearLayout) FindViewById(Resource.Layout.transferListRow);

        TextView s = (TextView) layout.FindViewById(Resource.Id.transferSerial);
        s.Text = serial;

        TextView m = (TextView) layout.FindViewById(Resource.Id.transferModel);
        m.Text = model;

        TextView st = (TextView) layout.FindViewById(Resource.Id.transferSite);
        st.Text = site;

        AddView(s);
        AddView(m);
        AddView(st);
    }
}

最終的な目標は、私の主な活動から、ボタンクリックイベントで次のようなことができるようになることです:

 mainLayout.AddView(new TransferListRow(this, "serial", "model", "site"));
4

1 に答える 1

4

1) LinearLayout を拡張しているため、XML で定義されたレイアウトに LinearLayout ラッパーを含める必要はありません。代わりに just を使用し<merge> ... </merge>ます。

2) コンストラクターで を呼び出す必要がありますinflator.Inflate(Resource.Layout.transferListRow, this);このパラメータに注意してください。これにより、XML で定義されたすべての要素が LinearLayout に子としてインフレートされます。

3) フレームワークは、インフレートが完了すると に通知onFinishInflate()し、ビューは で見つけることができますfindViewById()。したがって、onFinishInflate()TextView をメンバー変数に割り当てることができます。

それでおしまい :)

于 2012-07-02T15:37:43.570 に答える