【问题标题】:Android - No error but failed to insert fragmentAndroid - 没有错误但未能插入片段
【发布时间】:2016-06-20 16:53:19
【问题描述】:

我是 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();

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

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

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

【问题讨论】:

  • 似乎一切正常....您是否使用 android.support.v4.app.Fragment 以及将此代码放在主要活动中的位置
  • 是的,我正在使用android.support.v4.app.Fragment。我把最后一段代码放在MainActivity.java的onCreate()方法中。
  • 尝试更改 textview 的 textcolor 或将背景设置为 FrameLayout content_settings.xml,然后运行您的代码。
  • 哦,它确实出现了!在android设备上查看框架,是框架布局的问题:它填满了整个屏幕,并且文本隐藏在工具栏后面。
  • 感谢@KaranMer 的提示!

标签: java android android-fragments


【解决方案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,其中的文本位于工具栏后面。尝试添加更多文本,您会看到。要解决此问题,请使用上述解决方案。

【讨论】:

  • 就是这样!我也刚刚意识到这个问题,并试图使用 marginTop LOL。您的解决方案显然是一个更优雅的解决方案。谢谢!
【解决方案2】:

替换此行

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

到这里

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

【讨论】:

  • 试过了,还是和以前一样。但我有个问题。我打算将片段(即 content_settings.xml)插入到 app_bar_main.xml 中的 content_frame 中,而 app_bar_main.xml 又包含在 activity_main.xml 中。为什么我使用 activity_main 作为资源参数?
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 2020-02-03
  • 2019-03-16
  • 2011-02-10
  • 1970-01-01
  • 1970-01-01
  • 2011-06-22
  • 2021-12-07
相关资源
最近更新 更多