【问题标题】:How to get 2 fragments in a single activity如何在单个活动中获取 2 个片段
【发布时间】:2017-04-19 08:03:08
【问题描述】:

我正在尝试在 android 中使用片段并希望同时显示 fragment1fragment2 但使用此代码我只是得到第二个片段。如何获得两个碎片?

    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.Display;
    import android.view.WindowManager;

    public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            FragmentManager fm=getFragmentManager();
            FragmentTransaction ft= fm.beginTransaction();



                Fragment1 f1 = new Fragment1();
                ft.add(android.R.id.content, f1);
               Fragment2 f2 = new Fragment2();
                ft.add(android.R.id.content, f2);

        ft.commit();
        }
    }

【问题讨论】:

  • 你想如何显示 2 个片段?一个高于另一个?请详细说明。

标签: android android-studio android-fragments android-activity


【解决方案1】:

试试这个逻辑:

在您的主要活动布局中添加两个framelayout:一个接一个

 <?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:weightSum="2">

    <FrameLayout
        android:id="@+id/fragment1"
        android:name="com.example.FragmentOne"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <FrameLayout
        android:id="@+id/fragment2"
        android:name="com.example.FragmentTwo"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

现在在您的活动中,您可以使用以下方法添加两个片段:

Fragment1 one = new Fragment1();
Fragment2 two = new Fragment2();

FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.fragment1, one, "fragmentone").commit();
fm.beginTransaction().replace(R.id.fragment2, two, "fragmenttwo").commit();

【讨论】:

  • 当我使用 fm.beginTransaction().replace(R.id.fragment1, one, "fragmentone").commit(); fm.beginTransaction().replace(R.id.fragment2, 二, "fragmenttwo").commit();然后应用程序正在运行,但是当我使用 FragmentManager fm=getFragmentManager(); FragmentTransaction ft= fm.beginTransaction(); ft.replace(R.id.fragment1, one, "fragmentone").commit(); ft.replace(R.id.fragment2, 二, "fragmenttwo").commit();那么应用程序崩溃了。为什么?
  • 我没看懂你的意思............碎片交易版本是什么
  • 检查您是否使用v4版本?也贴一下崩溃日志。。没有日志就不容易知道崩溃的原因。。
【解决方案2】:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.Fragment1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment1"
        android:layout_weight="1" />

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout="@layout/fragment1"
        android:layout_weight="1" />

</LinearLayout>

【讨论】:

    猜你喜欢
    • 2016-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多