同一个片段的多个实例?

时间:2016-07-26 04:02:08

标签: c# android android-fragments xamarin xamarin.android

我正在使用Visual Studio和C#制作Android应用。我知道我应该可以使用Java,但我更喜欢C#,所以我选择使用它。但无论如何,我有一个问题,我不能两次添加相同的片段。我知道这是重复的,但其他任何问题都没有帮助。以下是我看过的内容:Adding multiple instances of the same fragment

除了只添加一个片段外,我的所有代码几乎都是一样的。这是我的代码:

protected override void OnCreate(Bundle bundle) {
    base.OnCreate(bundle);

    // Set our view from the "main" layout resource
    SetContentView(Resource.Layout.Main);

    var trans = FragmentManager.BeginTransaction();

    for(int i = 0; i < 5; i++)
        trans.Add(Resource.Id.bottomLayout, new BottomFragment(), "Fragment_" + i.ToString());

    trans.Commit();
}

Resource.Id.bottomLayout是一个垂直的LinearLayout,所以我不知道问题是什么。我觉得每个人都会生气,因为它是重复的(因为这总是发生在我身上,这就是为什么我使用这个网站作为最后的手段),但如果我能得到一些帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为所有五个片段都是直接相互叠加的。

android:orientation="vertical"android:orientation="vertical"应用于您的LinearLayout,以便让他们展开LinearLayout

垂直:

<LinearLayout
  android:id="@+id/bottomLayout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" />

水平:

<LinearLayout
  android:id="@+id/bottomLayout"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:orientation="horizontal" />
相关问题