2015-11-06 237 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插入到活动中。