[英]Why is my Fragment not replacing my Activity layout?
当我拿起从一个项目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>
有人可以告诉我我在这里想念什么吗?
我认为您可能对活动和片段有一些误解。 一般规则是:
在片段中构建用户界面(在大多数情况下,一个
Fragment
等于一个屏幕),然后使用“活动”来排列和显示这些片段。
带有NavigationDrawer
典型Activity
布局如下所示:
<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>
除了Fragment的占位符和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
替换为另一个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.