2017-03-05 75 views
0

的順序我有這樣的代碼:安卓:除了片段

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(moduli5包含在moduli2,moduli2被包含在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> 

回答

1

你的父母佈局是一個RelativeLayout,此佈局的默認行爲是第一個孩子的看法是最後一個子視圖下。

在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。

而且你的LinearLayout具有以下屬性:

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

所以他們把所有的地方可用。

嘗試在dp中設置固定大小而不是match_parent來查看真正發生的情況。

希望這會有所幫助。

對不起,我英文很差。

+0

當你說「這是相反的formazione3是formazione2」我不明白。所以我必須改變xml文件中的相關佈局的順序嗎? – Curio

+0

我的意思是formazione3是以上formazione2。是的,這是一個解決方案,它取決於你想要的最終結果。 – Cochi

+0

哦,是的,我明白了,我改變了xml文件中的相關佈局的順序!有用!但是,我故意放置了線性佈局的寬度和高度值。我在哪裏可以找到佈局如何添加容器?例如,您說在相對佈局中,訂單會根據書寫順序而變化。 – Curio