为什么我的Fragment没有替换我的Activity布局?

Dav*_*bji 2 java android android-fragments

当我从中选择一个项目NavigationDrawer并添加一个新的Fragment,FragmentTransaction它不会取代布局,Activity我无法弄清楚为什么!

这是我的布局Activity:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:layout_width="match_parent"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" >
    <!-- The main content view -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Location:"
        android:layout_marginTop="10dp"
        android:id="@+id/tv_disploc"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Temperature:"
        android:id="@+id/tv_disptemp"
        android:layout_below="@+id/tv_disploc"
        android:layout_marginTop="44dp" />


    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Search"
        android:onClick="ButtonClick"
        android:id="@+id/btn_search"
        android:layout_below="@+id/et_inloc"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="37dp"
        android:layout_gravity="center" />

    <EditText
        android:layout_width="174dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_inloc"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/tv_dispsearch"
        android:layout_gravity="center" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Search:"
        android:id="@+id/tv_dispsearch"
        android:layout_centerVertical="true"
        android:layout_gravity="left|center_vertical" />

    <EditText
        android:layout_width="126dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_outtemp"
        android:layout_below="@+id/et_outloc"
        android:layout_toRightOf="@+id/tv_disptemp"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="30dp"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"

        android:layout_gravity="center_horizontal|top" />

    <EditText
        android:layout_width="135dp"
        android:layout_height="wrap_content"
        android:id="@+id/et_outloc"
        android:layout_toRightOf="@+id/tv_disploc"
        android:layout_marginLeft="30dp"
        android:clickable="false"
        android:cursorVisible="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_gravity="center_horizontal|top" />
</FrameLayout>

    <!-- The navigation drawer -->
    <ListView android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

这就是Fragment我要显示的内容:

<LinearLayout
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:id="@+id/fragment_option"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:minWidth="1000dp"
    android:minHeight="1000dp">
    android:weightSum="1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Select your Default Location"
        android:layout_marginTop="100dp"
        android:layout_gravity="center_horizontal"
        android:id="@+id/textView" />

    <Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner_locations"
        android:layout_gravity="center_horizontal" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这里缺少什么?

Xav*_*ler 5

我想你可能对活动和碎片有一些误解.一般规则是:

在Fragments中构建用户界面(在大多数情况下,一个Fragment等于一个屏幕),然后使用Activities来排列和显示这些Fragments.

典型的Activity布局NavigationDrawer如下所示:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <!-- This is the placeholder for your Fragments --> 
    <!-- You can display your Fragments here with FragmentTransactions -->
    <FrameLayout
        android:id="@+id/flFragmentPlaceHolder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->
    <!-- It would even be better practice to use a Fragment here as well -->
    <!-- But since the Google Tutorials just use a ListView here I will as well -->
    <ListView android:id="@+id/lvDrawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

除了Fragments的占位符以外,还有其他任何内容NavigationDrawer都应该在该布局中.如果您Activity在当前状态下使用该布局,除了NavigationDrawer可以从左侧拉入之外,它基本上会显示一个空屏幕.您构建的任何用户界面都将在稍后通过Fragments添加.

Fragment在该布局中显示,您必须执行FragmentTransaction.您可以Fragments在一个单独添加,替换,删除,显示或隐藏多个FragmentTransaction.在上面的示例中,您可能希望向用户显示Fragment包含启动屏幕的内容.要做到这一点,你会执行FragmentTransaction如下操作:

// It is best practice to use factory methods to create Fragment instances
final Fragment fragment = StartupFragment.newInstance();

// There are multiple `FragmentManagers`, be sure to always use the right one!
FragmentManager manager = getFragmentManager();

// This starts the `FragmentTransaction`.
FragmentTransaction transaction = manager.beginTransaction();

// Now you can define what happens in this transactions
// You can add/replace/remove/hide or show as many Fragments 
// as you want in one `FragmentTransaction`.
// This command specifically adds the Fragment to the placeholder we defined
// in the layout of the Activity
transaction.replace(R.id.flFragmentPlaceHolder, fragment);

// This commits the `FragmentTransaction`.
// Only after you call this will any changes be made
transaction.commit();
Run Code Online (Sandbox Code Playgroud)

为简洁起见,上述相同的代码也可以写成:

final Fragment fragment = StartupFragment.newInstance();
getFragmentManager().beginTransaction()
        .replace(R.id.flFragmentPlaceHolder, fragment)
        .commit();
Run Code Online (Sandbox Code Playgroud)

这样写它的主要改进之处是,你作为一个开发者可以写很多更快,因为你可以依靠代码完成几乎全部的时间.

如果用户稍后从中选择一个项目,NavigationDrawer那么您只需要执行一个新项目FragmentTransaction来替换StartupFragment另一个项目!