我想动态地添加一个布局到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。
我所做的是欺骗一点,并在ScrollView中的现有布局中添加我的动态布局。下面是一个HorizontalScrollView:
的例子<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);
}