我有此代码:
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();
}
});
好吧,我正在体验片段的添加顺序。我有4个片段,我不明白为什么有些片段添加时,将它们放在较大的片段下,而其他片段则在其容器中的较大碎片上。
formazione3> formazione2(formazione2包含在formazione3中)moduli2> moduli5(模量在模量中包含,模量在formazione3中部分包含)
当我添加第二片片段时,它没有显示出来,我认为它属于上一个片段,因此它在较大的情况下。当我添加第三个片段时,它部分放在第一个片段下,第一个片段更大。当我添加第四个片段时,它会在第三个片段中,但是第三个比第四片大,最重要的是第三个包含在第三个。它是如何工作的?我一点都不明白!
这是布局:
<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>
您的父级布局是一个相关性,这种布局的默认行为是第一个子查看在最后一个孩子视图下。
在您的XML中,这个孩子:
<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="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>
因此,当视图膨胀时,formazione3与formazione2相反。
此外,您的线层具有以下属性:
android:layout_width="match_parent"
android:layout_height="match_parent"
因此他们占据了所有可用的位置。
尝试将fixe大小设置为DP而不是match_parent以查看真正发生的事情。
希望这会有所帮助。
对不起,我的英语不好。