2017-08-14 127 views
-1

我想在一個單一的佈局像這樣加2個片段:如何在單個佈局中動態添加2個片段?

Two Fragments

我可以使用靜態片段的方法,但不是能夠動態地做到這一點做到這一點。我想要定位API 14及更高版本。

下面是代碼:

  1. activity_main.xml中:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:layout_width="fill_parent" 
 
    android:layout_height="fill_parent" 
 
    android:orientation="vertical"> 
 

 

 
    <FrameLayout 
 
     android:id="@+id/new_placeholder" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent"> 
 

 
    </FrameLayout> 
 

 
    <FrameLayout 
 
     android:id="@+id/my_placeholder" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="match_parent" 
 
     > 
 

 
    </FrameLayout> 
 

 
</LinearLayout>

  • fragment1.xml:
  • <?xml version="1.0" encoding="utf-8"?> 
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     
        android:layout_width="match_parent" 
     
        android:layout_height="match_parent" 
     
        android:orientation="vertical" 
     
        android:background="#00ff00" 
     
        > 
     
    
     
        <TextView 
     
         android:id="@+id/textView1" 
     
         android:layout_width="wrap_content" 
     
         android:layout_height="wrap_content" 
     
         android:text="fragment first" 
     
         android:textAppearance="?android:attr/textAppearanceLarge" 
     
         android:layout_weight="1" 
     
         /> 
     
    
     
    </LinearLayout>

  • fragment2.xml:
  • <?xml version="1.0" encoding="utf-8"?> 
     
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     
        android:layout_width="match_parent" 
     
        android:layout_height="match_parent" 
     
        android:orientation="vertical" 
     
        android:background="#0000ff"> 
     
    
     
        <TextView 
     
         android:id="@+id/textView2" 
     
         android:layout_width="wrap_content" 
     
         android:layout_height="wrap_content" 
     
         android:text="fragment second" 
     
         android:textAppearance="?android:attr/textAppearanceLarge" 
     
         android:layout_weight="1" 
     
         /> 
     
    
     
    </LinearLayout>

  • 片段1。 java:
  • package org.hinduismfacts.www.dynamicfragment; 
     
    
     
    import android.app.Fragment; 
     
    import android.os.Bundle; 
     
    import android.view.LayoutInflater; 
     
    import android.view.View; 
     
    import android.view.ViewGroup; 
     
    
     
    /** 
     
    * Created by Rahul on 07-08-2017. 
     
    */ 
     
    
     
    public class fragment1 extends Fragment { 
     
        @Override 
     
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     
              Bundle savedInstanceState) { 
     
         // TODO Auto-generated method stub 
     
         return inflater.inflate(R.layout.fragment1,container, false); 
     
        } 
     
    
     
    }

  • fragment2.java:
  • package org.hinduismfacts.www.dynamicfragment; 
     
    
     
    import android.app.Fragment; 
     
    import android.os.Bundle; 
     
    import android.view.LayoutInflater; 
     
    import android.view.View; 
     
    import android.view.ViewGroup; 
     
    
     
    /** 
     
    * Created by Rahul on 07-08-2017. 
     
    */ 
     
    
     
    public class fragment2 extends Fragment { 
     
        @Override 
     
        public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     
              Bundle savedInstanceState) { 
     
         // TODO Auto-generated method stub 
     
         return inflater.inflate(R.layout.fragment2,container, false); 
     
        } 
     
    
     
    }

  • MainActivity .java:
  • package org.hinduismfacts.www.dynamicfragment; 
     
    
     
    import android.app.Fragment; 
     
    import android.app.FragmentManager; 
     
    import android.app.FragmentTransaction; 
     
    import android.support.v7.app.AppCompatActivity; 
     
    import android.os.Bundle; 
     
    import android.view.View; 
     
    import android.widget.TextView; 
     
    
     
    public class MainActivity extends AppCompatActivity { 
     
    
     
        @Override 
     
        protected void onCreate(Bundle savedInstanceState) { 
     
         super.onCreate(savedInstanceState); 
     
         setContentView(R.layout.activity_main); 
     
    
     
         FragmentManager fragmentManager = getFragmentManager(); 
     
         FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     
    // Create new fragment and transaction 
     
         Fragment newFragment = new fragment1(); 
     
         Fragment myFragment = new fragment2(); 
     
    
     
         FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
     
    
     
    // Replace whatever is in the fragment_container view with this fragment, 
     
    // and add the transaction to the back stack 
     
         transaction.add(R.id.new_placeholder, newFragment); 
     
         transaction.add(R.id.my_placeholder, myFragment); 
     
    
     
         transaction.commit(); 
     
    
     
        } 
     
    }

    在輸出我得到只顯示第一個片段。第二個片段沒有被添加。輸出:

    Output

    能否請你告訴我什麼錯誤?

    +0

    第一張圖片顯示水平方向。代碼表示垂直方向。請清除你的問題 –

    +0

    對不起,我很困惑。其實,我從網站上覆制了這些內容,因爲我是Android新手,我認爲這是正確的。 – Rahul

    回答

    1

    請加入android:layout_weight="1"

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:orientation="horizontal"> 
    
    
        <LinearLayout 
         android:id="@+id/new_placeholder" 
         android:layout_width="match_parent" 
         android:layout_weight="1" 
         android:layout_height="match_parent"> 
    
        </LinearLayout> 
    
        <LinearLayout 
         android:id="@+id/my_placeholder" 
         android:layout_width="match_parent" 
         android:layout_weight="1" 
         android:layout_height="match_parent" 
         > 
    
        </LinearLayout> 
    
    </LinearLayout> 
    
    +0

    謝謝,它解決了這個問題。 – Rahul

    0

    問題出在您的activity_main.xml中。 使用android:layout_weight="1"爲每個碎片容器在屏幕上顯示它們。

    另外fill_parent已棄用。考慮使用match_parent代替

    1

    您需要佈局重android:layout_weight爲您的框架佈局 試試下面XML改變

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:weightSum="2" 
        android:orientation="horizontal"> 
    
        <FrameLayout 
         android:id="@+id/new_placeholder" 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="match_parent"> 
        </FrameLayout> 
    
        <FrameLayout 
         android:id="@+id/my_placeholder" 
         android:layout_width="0dp" 
         android:layout_weight="1" 
         android:layout_height="match_parent"> 
        </FrameLayout> 
    
    </LinearLayout> 
    
    +0

    首先,在上面的問題中檢查圖片,他想用寬度來劃分 – akhilesh0707

    +0

    難道你不覺得,在移動設備上的水平方向會有點尷尬嗎?而且他提供了垂直方向 –