C# 如何将元素添加到特定元素的子元素?

C# 如何将元素添加到特定元素的子元素?,c#,android,xamarin,layout-inflater,C#,Android,Xamarin,Layout Inflater,我正在开发一个简单的时间表应用程序,我将日期表示为线性布局,主题表示为按钮。我希望通过膨胀以编程方式生成日期和主题,因此我为day元素和主题创建了2个AXML: 带标题的日期: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:m

我正在开发一个简单的时间表应用程序,我将日期表示为线性布局,主题表示为按钮。我希望通过膨胀以编程方式生成日期和主题,因此我为day元素和主题创建了2个AXML:

带标题的日期:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/linearLayoutDay"
android:layout_margin="20px">
<Button
android:text="Day"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/IDDay"
android:textAlignment="center"
android:layout_marginBottom="20px" />
</LinearLayout>

主题:

   <?xml version="1.0" encoding="utf-8"?>
   <Button xmlns:android="http://schemas.android.com/apk/res/android"
    android:text="Large Text1"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView" />

我可以膨胀任意数量的新日元素,以便将它们添加到预定义的线性布局中,并且我可以使用以下代码添加主题:

//adding a new day element
View day = LayoutInflater.Inflate(Resource.Layout.DayElement, FindViewById<LinearLayout>(Resource.Id.linearLayoutTimeTable));

//adding a subject to the day
View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, FindViewById<LinearLayout>(day.Id));
//添加新的day元素
查看日期=LayoutFlater.充气(Resource.Layout.DayElement,FindViewById(Resource.Id.linearLayoutTimeline));
//为当天添加主题
视图主题=LayoutInflater.Inflate(Resource.Layout.SubjectEntry,FindViewById(day.Id));
我的问题是,我想将按钮添加到表示日期的axml的线性布局子级,类似于(day.Id.specificElement)。我可以直接引用infalted day元素的线性布局子元素:

View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, FindViewById<LinearLayout>(Resource.Id.linearLayoutDay));
View subject=LayoutInflater.Inflate(Resource.Layout.SubjectEntry,FindViewById(Resource.Id.linearLayoutDay));
但是通过这种方式,所有Buton都被添加到第一天元素中,我需要一种方法来将这个infalted button元素添加到先前膨胀元素的子元素中。我应该如何处理这个问题

更新 我还尝试将上下文设置为第二天元素,但按钮还是添加到了第一天元素中:

LinearLayout layout = day2.FindViewById<LinearLayout>(Resource.Id.linearLayoutDay);
View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, layout);
LinearLayout布局=day2.FindViewById(Resource.Id.linearLayoutDay);
查看主题=LayoutInflater.flate(Resource.Layout.SubjectEntry,Layout);
更新 似乎有关LinearLayout布局的代码并不意味着在指定父视图(在本例中为第2天)的视图中查找指定ID为的视图,正如我之前所怀疑的那样。
我来自一个C#Unity的背景,每个元素都可以以类似的方式访问,我希望在不同的day元素(比如day1.linearLayoutDay、day2.linearLayoutDay等)中有许多具有相同ID的元素,这样我就可以轻松访问它们,但C#Xamarin.Android似乎不是这样运行的。我做错了什么?

我想我找到了一个解决方案,虽然它一点也不优雅,但目前似乎还有效,所以我决定把它贴在这里,以防有人发现它有用。 在谷歌搜索了几个小时后,我意识到我的XamarinXML知识根本不足以以这种方式解决问题,因此我发掘了knoweldge背后的Unity代码,并想出了一些非常简单的方法。以下代码将5个线性布局生成为预先存在的父布局,然后使用设计的xml文件中的4个按钮填充它们:

for (int i = 0; i < 5; i++)
        {
            //add new elements to a collection
            week.Add(LayoutInflater.Inflate(Resource.Layout.DayElement, FindViewById<LinearLayout>(Resource.Id.linearLayoutTimeTable)));
            //define new LL
            LinearLayout lin = new LinearLayout(this);
            //assign the new LL the desired xml
            lin = FindViewById<LinearLayout>(Resource.Id.linearLayoutDay);
            //set its ID to smth fathomable
            lin.Id = i;

            //add 4 subjects of the predefined xml to every LL
            for (int j = 0; j < 4; j++)
            {
                View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, FindViewById<LinearLayout>(i));
            }
        }   
for(int i=0;i<5;i++)
{
//向集合中添加新元素
添加(LayoutInflater.Inflate(Resource.Layout.DayElement,FindViewById(Resource.Id.linearLayoutTimeline));
//定义新的LL
LinearLayout lin=新的LinearLayout(本);
//为新的LL分配所需的xml
lin=findviewbyd(Resource.Id.linearLayoutDay);
//将其ID设置为smth fathomable
lin.Id=i;
//将预定义xml的4个主题添加到每个LL
对于(int j=0;j<4;j++)
{
视图主题=LayoutInflater.Inflate(Resource.Layout.SubjectEntry,FindViewById(i));
}
}   
这似乎解决了如何使用可以正确引用的唯一ID扩展同一xml元素的问题。 请随意批评,我仍然对如何从xml优雅地实现这一点感兴趣(如果可能的话)。当然,为了能够对其进行数据绑定,必须对其进行调整