2015-11-06 160 views
-1

我是新來的android和我有一些問題。我創建了一個簡單的佈局有兩個片段,例如如下:片段活動片段內

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

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

<FrameLayout 
    android:id="@+id/fragment2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" /> 

我要劃分這個佈局如下:1/3高度

layout_height的:

片段1的layout_height片段2:2/3高度

怎麼辦?

我用下面的代碼顯示片段1:

FragmentTransaction transaction = 
      getSupportFragmentManager().beginTransaction(); 

    transaction.add(R.id.fragment1, firstfragment); 

但我不知道如何表達FragmentActivity在fragment2?

回答

0
You can achive this layout like this 

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

    <FrameLayout 
     android:id="@+id/fragment1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <FrameLayout 
     android:id="@+id/fragment2" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="2" /> 

</LinearLayout> 

,並在Java中,你必須調用事務提交

//添加片段1

FragmentTransaction交易= getSupportFragmentManager()的BeginTransaction()。 transaction.add(R.id.fragment1,firstfragment).commit();

//添加Fragment2

FragmentTransaction transaction1 = 
     getSupportFragmentManager().beginTransaction(); 
transaction1.add(R.id.fragment2, secondfragment).commit; 

,你不能把任何活動中的片段。但你可以從你的片段開始另一項活動

0

爲了把兩個元件在一些比 - 使用的LinearLayout然後設置的載視圖0dp高度並添加layoutWeight在期望的比例屬性(例如1和2中的情況下) 要實例片段只是用代碼作爲你寫入兩次。您省略了transaction.commit();部分。你必須調整兩次 - 一次爲你想添加的每個片段。

關於添加FragmentActivity的問題會引起誤解 - 您不能將Activity放入Fragment中,只能將Fragment插入到活動中。