Android layout android版mono-动态向scrollview添加布局

Android layout android版mono-动态向scrollview添加布局,android-layout,xamarin.android,scrollview,Android Layout,Xamarin.android,Scrollview,我想在ScrollView中动态添加布局。我尝试了在谷歌和这里找到的一切,但没有机会!其中大多数都会导致“对象引用”错误。以下是其中一个代码: LayoutInflater layoutInflater = (LayoutInflater) this.GetSystemService(Context.LayoutInflaterService); View view = new View(this); view = layoutInflater.Inflate(Resource.Layo

我想在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=“示例”

我要做的是作弊,在ScrollView中的现有布局中添加我的动态布局。下面是一个带有水平滚动视图的示例:

<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);
}
LinearLayout messageListView=findviewbyd(Resource.Id.messageHolder);
foreach(对象中的var对象){
//在这里创建动态视图。
视图=新视图(此);
view=LayoutFlater.Inflate(Resource.Layout.MYLAYOUT,null);
TextView internalTextView=view.FindViewById(Resource.Id.internalTextView);
internalTextView.SetText(“Hello world!”,TextView.BufferType.Normal);
messageListView.AddView(视图);
}

我要做的是稍微作弊,将我的动态布局添加到ScrollView中的现有布局中。下面是一个带有水平滚动视图的示例:

<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);
}
LinearLayout messageListView=findviewbyd(Resource.Id.messageHolder);
foreach(对象中的var对象){
//在这里创建动态视图。
视图=新视图(此);
view=LayoutFlater.Inflate(Resource.Layout.MYLAYOUT,null);
TextView internalTextView=view.FindViewById(Resource.Id.internalTextView);
internalTextView.SetText(“Hello world!”,TextView.BufferType.Normal);
messageListView.AddView(视图);
}

您从哪里获得
异常
?为什么要实例化一个
视图
,然后立即放弃它?我在MYSCROLLVIEW.AddView(View)中得到了错误;我调试了代码,在这一行,“视图”对象似乎是空的!!!您从哪里获得
异常
?为什么要实例化一个
视图
,然后立即放弃它?我在MYSCROLLVIEW.AddView(View)中得到了错误;我调试了代码,在这一行,“视图”对象似乎是空的!!!非常感谢!那有帮助!但是现在,我如何访问新视图的元素?!例如,我在child中有一个textview,我想为它设置一个text。经过编辑,在该视图中包含您的
视图
,并带有一个textview示例。非常感谢!那有帮助!但是现在,我如何访问新视图的元素?!例如,我在child中有一个textview,我想为它设置一个text。经过编辑,在该视图中包含您的
视图
,其中包含一个textview示例。