【问题标题】:FragmentActivity inside a fragment片段内的 FragmentActivity
【发布时间】:2015-11-06 05:44:56
【问题描述】:

我是 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" />

我想将此布局划分如下:

fragment1 的布局高度:1/3 高度

fragment2 的布局高度:2/3 高度

怎么办?

我用下面的代码来显示片段1:

 FragmentTransaction transaction =
            getSupportFragmentManager().beginTransaction();

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

但我不知道如何在 fragment2 中显示 FragmentActivity ?

【问题讨论】:

    标签: android fragment android-fragmentactivity


    【解决方案1】:
    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中你必须在事务中调用commit

    //添加Fragment1

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

    //添加Fragment2

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

    而且您不能在 Fragments 中放置任何活动。但是你可以从你的片段开始另一个活动

    【讨论】:

      【解决方案2】:

      要以某种比例放置两个元素 - 使用 LinearLayout 然后将包含视图的高度设置为 0dp 并以所需的比例添加 layoutWeight 属性(例如,在您的情况下为 1 和 2) 要实例化片段,只需使用您编写两次的代码。您省略了 transaction.commit(); 部分。您必须对此进行两次校准 - 为您要添加的每个片段一次。

      关于添加 FragmentActivity 的问题具有误导性 - 您不能将 Activity 放入 Fragment,您只能将 Fragment(s) 插入到活动中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多