片段布局未添加更多视图

时间:2016-12-19 20:28:17

标签: android android-layout android-fragments android-nested-fragment

我正在尝试在我的片段布局中添加FrameLayout,只是为了保留空间以编程方式添加另一个片段。这是代码:

FRAGMENT XML LAYOUT

<?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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/detail"
        android:text="" />

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

</LinearLayout>

但问题是,当我使用FragmentTransaction 将<{em>}替换为FrameLayout时,会出现错误:

FATAL EXCEPTION: main
              Process: com.platform.apple, PID: 2448
              java.lang.IllegalArgumentException: No view found for id 0x7f080005 (com.platform.apple:id/fragment_container) for fragment Stopwatch1Fragment{e0be34c #0 id=0x7f080005}

这是Fragment类代码:

片段类代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
    return inflater.inflate(R.layout.fragment_apple_detail, container, false);
}

现在,当我尝试使用R.id.detail代替R.id.fragment_container时,片段类在我创建另一个TextView时发现了TextView片段的xml布局中的FrameLayout之后并尝试使用它,它仍然无法正常工作(片段未通过它的Id找到视图)。所以看起来只有第一个视图 R.id.detail )正在工作,而 Fragment类找不到任何其他添加的之后查看。请帮忙!

2 个答案:

答案 0 :(得分:0)

尝试按照

复制onCreateViewcode
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

    View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();

    return root;
}

答案 1 :(得分:0)

将宽度和高度设置为片段xml layoug中的匹配父项,如下所示

<?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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/detail"
    android:text="" />

<FrameLayout
    android:layout_width="match_content"
    android:layout_height="match_content"
    android:id="@+id/fragment_container"/>

然后以编程方式初始化片段,如下面

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                 Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
transaction.replace(R.id.fragment_container, stopwatch1Fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();

return root;
}