彼此相邻添加两个片段

时间:2016-08-17 10:22:38

标签: android android-fragments

我想在活动中彼此相邻地添加两个片段,但是当我尝试这样做时,我得到异常:( Fragment1没有创建视图) 这段代码有什么不对。

MainActivity.java

public class MainActivity extends ActionBarActivity {

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

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

    // get the display mode
    int displaymode = getResources().getConfiguration().orientation;
    if (displaymode == 1) { // it portrait mode
        Fragment1 f1 = new Fragment1();
        fragmentTransaction.replace(R.id.frag_1, f1);
    } else {// its landscape
        Fragment1 f1 = new Fragment1();
        fragmentTransaction.add(R.id.frag_1, f1);
        Fragment2 f2 = new Fragment2();
        fragmentTransaction.add(R.id.frag_2, f2);
    }
    fragmentTransaction.commit();
  }
}

activity_main.xml中

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"    >
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag_1"
    class="com.example.android.myapplication.Fragment1"/>
<fragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frag_2"
    class="com.example.android.myapplication.Fragment2"/>

fragment1.java

    public class Fragment1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup vg,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_fragment1, vg, false);
}

}

fragment2.java

     public class Fragment2 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup vg,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_fragment2, vg, false);
}

}

fragment_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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="I m fragment ONE"
    android:gravity="center"
    android:background="#5eff6a"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

fragment_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" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="I m fragment TWO"
    android:gravity="center"
    android:background="#ff9e5e"
    android:textAppearance="?android:attr/textAppearanceLarge" />

    </LinearLayout>

5 个答案:

答案 0 :(得分:0)

尝试使用FrameLayout而不是xml中的片段。

答案 1 :(得分:0)

由于您是直接在布局中添加片段,因此无需在活动中添加它们。

答案 2 :(得分:0)

Android设备存在各种屏幕尺寸和密度。

面板或窗格代表用户界面的一部分。术语窗格是一个通用术语,用于描述多个视图根据实际可用空间组合到一个复合视图中的概念。

如果空间不足,则只显示一个面板。这通常称为单窗格布局。

如果有更多空间,可以显示多个面板。

此链接可能会对您有所帮助:http://www.vogella.com/tutorials/AndroidFragments/article.html

答案 3 :(得分:0)

使用getSupportFragmentManager()。beginTransaction();

并按照以下链接进行操作,该链接在Android开发者网站上提供了文档:

https://developer.android.com/training/basics/fragments/fragment-ui.html

答案 4 :(得分:0)

问题是您对两个片段使用 match_parent 。您应该在横向上使用带有固定dpis的断点(例如,值-w600dp / ...),并在纵向上使用match_parent。

顺便说一句,我认为您的问题可以通过使用master/detail activity(当然使用片段)的更好代码来解决。

相关问题