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

时间:2014-11-27 20:29:38

标签: 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>

这是我要显示的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>

有人可以告诉我这里缺少什么吗?

1 个答案:

答案 0 :(得分:5)

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

  

在Fragments中构建用户界面(在大多数情况下,一个Fragment等于一个   屏幕)然后使用活动来安排和显示这些   片段。

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>

除了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();

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

final Fragment fragment = StartupFragment.newInstance();
getFragmentManager().beginTransaction()
        .replace(R.id.flFragmentPlaceHolder, fragment)
        .commit();

这样编写它的主要改进是,作为开发人员,您可以更快地编写很多,因为您几乎可以在整个时间内完全依赖代码。

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