1

ScrollView にレイアウトを動的に追加したいと考えています。私はグーグルとここで見つけたすべてを試しましたが、チャンスはありません! それらのほとんどは、「オブジェクト参照」エラーを引き起こします。コードの 1 つを次に示します。

LayoutInflater layoutInflater = (LayoutInflater) this.GetSystemService(Context.LayoutInflaterService);    
View view = new View(this);
view = layoutInflater.Inflate(Resource.Layout.MYLAYOUT, null);
MYSCROLLVIEW.AddView(view);

ただし、「オブジェクト参照がオブジェクトのインスタンスに設定されていません」が発生します。

その後、MYLAYOUT 内のコントロール (ビュー) を使用したいと思います。たとえば、次のようになります。

MYLAYOUT.textView1.Text = "例";

4

1 に答える 1

1

私がしていることは、少しごまかして、ScrollView 内の既存のレイアウト内に動的レイアウトを追加することです。次に、Horizo​​ntalScrollView の例を示します。

<HorizontalScrollView
    android:layout_width="739dp"
    android:layout_height="match_parent"
    android:id="@+id/horizontalMessageScrollView"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dp"
    android:scrollbars="none">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/messageHolder" />
</HorizontalScrollView>

そして、私のコードでは次を使用します。

LinearLayout messageListView = FindViewById<LinearLayout>(Resource.Id.messageHolder);

foreach (var object in objects) {  
    // create your dynamic views here.      

    View view = new View(this);
    view = layoutInflater.Inflate(Resource.Layout.MYLAYOUT, null);

    TextView internalTextView= view.FindViewById<TextView>(Resource.Id.internalTextView);
    internalTextView.SetText("Hello world!", TextView.BufferType.Normal);
    messageListView.AddView(view);
}
于 2013-03-08T18:34:19.307 に答える