java - Android: fragment 的添加顺序

标签 java android android-fragments

我有这个代码:

 findViewById(R.id.button2).setOnClickListener(new View.OnClickListener()
    {
        //Se aggiungo un fragment A al container X e aggiungo il fragment B allo stesso container, il fragment B andrà sopra
        //il fragment A
        @Override
        public void onClick(View v)
        {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if(ko==0)
            {
                MyFragment fragment = MyFragment.createFragment(0);
                fragmentTransaction.add(R.id.formazione3,fragment);
                ko++;
            }

            else if(ko==1)
            {
                MyFragment fragment = MyFragment.createFragment(1);
                fragmentTransaction.add(R.id.formazione2,fragment);
                ko++;
            }

            else if(ko==2)
            {
                MyFragment fragment = MyFragment.createFragment(2);
                fragmentTransaction.add(R.id.moduli2,fragment);
                ko++;
            }

            else if(ko==3)
            {
                MyFragment fragment = MyFragment.createFragment(1);
                fragmentTransaction.add(R.id.moduli5,fragment);
            }

            fragmentTransaction.commit();
        }
    });

嗯,我正在体验 fragment 的添加顺序。我有 4 个 fragment ,我不明白为什么当我添加它们时,有些 fragment 会放在较大的 fragment 下,而其他 fragment 会放在容器中的较大 fragment 上。

formazione3>formazione2(formazione2 包含在 formazione3 中) moduli2>moduli5(moduli5包含在moduli2中,moduli2部分包含在formazione3中)

当我添加第二个 fragment 时,它没有显示,我认为它位于前一个 fragment 下方,因此它位于更大的 fragment 下方。当我添加第三个 fragment 时,它部分位于第一个 fragment 下方,第一个 fragment 更大。当我添加第四个 fragment 时,它会出现在第三个 fragment 上,但第三个 fragment 比第四个 fragment 大,而且最重要的是,第四个 fragment 包含在第三个 fragment 中。它是如何工作的?我完全不明白!

这是布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.utente.fantacalcio.FormazioniActivity"
android:weightSum="1"
android:id="@+id/activity_formazioni_layout">

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="12dp"
    android:id="@+id/button2" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/button" />

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <RelativeLayout
        android:layout_width="107dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="92dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli1">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione1">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="71dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli2">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione2">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="51dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli3">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione3">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="29dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli4">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione4">

    </RelativeLayout>
</LinearLayout>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="11dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli5">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione5">

    </RelativeLayout>
</LinearLayout>

最佳答案

您的父布局是 RelativeLayout ,此布局的默认行为是第一个 subview 位于最后一个 subview 下方。

在你的 xml 中这个 child :

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="51dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli3">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione3">

    </RelativeLayout>
</LinearLayout>

在此 subview 下:

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="71dp"
        android:layout_height="match_parent"
        android:id="@+id/moduli2">

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/formazione2">

    </RelativeLayout>
</LinearLayout>

因此,当 View 膨胀时,这与 formazione3 相对于 formazione2 相反。

此外,您的 LinearLayout 具有以下属性:

android:layout_width="match_parent"
android:layout_height="match_parent"

所以他们占据了所有可用的位置。

尝试在 dp 中设置固定大小而不是 match_parent 来看看到底发生了什么。

希望这有帮助。

抱歉我的英语不好。

关于java - Android: fragment 的添加顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607024/

相关文章:

java - 为什么我无法使用 JPA 连接数据库?

java - Talend 开放工作室 - 如何进行下一步

Android Studio 更新 SDK - 完成。没有安装任何东西

android - red5(server) 和 android(client) 之间如何进行视频聊天?

java - 使用 Vector3 的加速逻辑

java - Hibernate - 插入具有一对多关系的数据

android - 如何包装内容 View 而不是背景可绘制?

android - 如何在 fragment 中膨胀另一个 View ?

Android EditText无法输入文字

安卓 : Fullscreen system visibility screws up window insets