1

我想将布局动态添加到 ScrollView 中。我尝试了我在谷歌和这里找到的所有东西,但没有机会!它们中的大多数会导致“对象引用”错误。这是代码之一:

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