【发布时间】: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