为什么我的片段没有取代我的活动布局?

2024-06-26

当我从NavigationDrawer并添加一个新的Fragment with a 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>

有人能告诉我我在这里缺少什么吗?


我认为您可能对 Activity 和 Fragments 有一些误解。一般规则是:

在片段中构建您的用户界面(在大多数情况下,一个Fragment等于一 screen),然后使用 Activity 来排列和显示这些 碎片。

一个典型的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>

除了片段的占位符和片段的必要部分之外没有其他任何东西NavigationDrawer应该在那个布局中。如果您想在Activity在当前状态下,除了NavigationDrawer可以从左侧拉入。您构建的任何用户界面稍后都将通过片段添加。

要显示一个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();

这样写的主要改进是你作为开发人员可以写它a lot速度更快,因为您几乎可以一直依赖代码完成。

如果稍后用户从NavigationDrawer那么你只需要执行一个新的FragmentTransaction来替换StartupFragment与另一个不同的!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的片段没有取代我的活动布局? 的相关文章

随机推荐