Android - 没有错误,但未能插入片段

时间:2016-03-07 07:51:07

标签: java android android-fragments

我是Android应用中的新手,我现在正尝试将片段插入<FrameLayout>。这是我的代码:

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

    <include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
tools:context="ycl.com.remotearduino.MainActivity">

    <android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>

content_settings.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Ho man!" />

</FrameLayout>

SettingsFragment.java的一部分:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.content_settings, container, false);
}

MainActivity.java的onCreate的一部分:

SettingsFragment settingsFragment = new SettingsFragment();

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.content_frame, settingsFragment).commit();

当我运行应用程序时,没有错误,但片段根本没有出现。有人知道为什么会这样吗?

编辑 :我注意到onCreateView中的方法膨胀根本没有执行,因为我现在处于DEBUG模式但标签“LayoutInflator”没有显示起来。它应该根据LayoutInflater源代码打印'INFLATING from resource:...'

编辑 :onCreateView()确实已执行,但仍然没有inflate()方法的迹象......

2 个答案:

答案 0 :(得分:1)

将其添加到FrameLayout:

 <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

您的SettingsFragment可见,但由于CoordinatorLayout,其中的文本位于工具栏后面。尝试添加更多文本,您将看到。要解决此问题,请使用上述解决方案。

答案 1 :(得分:0)

替换此行

inflater.inflate(R.layout.content_settings, container, false);

到这个

inflater.inflate(R.layout.activity_main, container, false);