如何将元素添加到特定膨胀元素的 children?

How to add element to children of a specific inflated element?

我正在开发一个简单的时间表应用程序,我将日期表示为线性布局,将主题表示为按钮。我想通过 inflation 以编程方式生成日期和主题,因此我为日期元素和主题创建了 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));

我的问题是我想将按钮添加到表示日期的 axml 的 线性布局 child 中,例如 (day.Id.specificElement) .我可以直接引用infalted day元素的Linear Layout child:

View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, FindViewById<LinearLayout>(Resource.Id.linearLayoutDay));

但是通过这种方式,所有的按钮都被添加到第一天的元素中,我需要一种方法来将这个膨胀的按钮元素添加到先前膨胀的元素的 child 中。我该如何处理?

更新

我也尝试将上下文设置为第二天元素,但按钮仍然添加到第一个:

LinearLayout layout = day2.FindViewById<LinearLayout>(Resource.Id.linearLayoutDay);
View subject = LayoutInflater.Inflate(Resource.Layout.SubjectEntry, layout);

更新

关于LinearLayout布局的代码似乎并不意味着在指定的视图中找到具有指定ID的视图 parent 视图(在本例中为第 2 天),正如我之前所怀疑的。 来自 C# Unity 背景,其中每个元素都可以以类似的方式访问,我希望在不同的日期元素中有许多具有相同 ID 的此类元素(所以 day1.linearLayoutDay、day2.linearLayoutDay 等)所以我可以轻松访问它们,但这不是 C# Xamarin.Android 的运行方式。我做错了什么?

我想我找到了一个解决方案,虽然它一点也不优雅,但它现在似乎可以工作,所以我决定在这里 post 它,以防有人发现它有用。 经过几个小时的谷歌搜索,我意识到我的 Xamarin xml 知识根本不足以解决问题,所以我挖掘了我的 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));
            }
        }   

这似乎解决了如何使用可以正确引用的唯一 ID 扩充同一个 xml 元素的问题。 请随意批评这一点,我仍然对如何从 xml 优雅地做到这一点感兴趣(如果可能的话)。当然,必须对此进行调整才能对其进行数据绑定。