交易后片段重叠
Fragments overlap after transaction
我将应用程序的 LauncherActivity 设为 NavigationDrawer Activity。然后我制作了两个测试片段:"VibBearFragment" 和 "FragmentTwo"
我在 onNavigationItemSelected() 中使用此代码在片段之间切换。我将 "VibBearFragment" 包含在抽屉的内容中,以便在应用程序启动时它就在那里。
Fragment fragment = new FragmentTwo();
switch (item.getItemId()) {
case R.id.nav_teddy_vib:
fragment = new VibBearFragment();
break;
case R.id.nav_fragment1:
break;
case R.id.vibActivity:
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
break;
case R.id.nav_call_me:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:0123456789"));
startActivity(callIntent);
break;
default:
fragment = new FragmentTwo();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.vibFragment, fragment).commit();
但是 "VibBearFragment" 只是掩盖了 "FragmentTwo" 并且它们 overlap.And 当 Fragments 是 overlapping.The 时所有的按钮都有效 overlapping.The 有趣的是,如果我点击 "FragmentTwo" 它们重叠,但如果我单击 "VibBearFragment",即使 "FragmentTwo" 已打开,它也能正常工作。
这是被语法替换的 XML 片段
<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.slaven.teddybear.Fragments.VibBearFragment"
android:id="@+id/vibFragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/vib_bear_fragment" />
我已尽力向您展示问题所在。还有按钮 N2(来自图片)
当活动重叠时消失,但仍然works.Can有人帮忙吗?
您不能使用静态片段(在 xml 布局中定义的片段)进行交易。如果您确实打算进行交易,请手动添加它们。
<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">
<FrameLayout android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
然后在您的活动 onCreate() 方法中添加来自布局的片段:
//...onCreate()
Fragment content = getSupportFragmentManager().findFragmentById(R.id.content);
if (savedInstance == null || content == null) {
// add the initial fragment from the layout
content = new VibBearFragment();
getSupportFragmentmanager().beginTransaction().replace(R.id.content, content).commit();
}
我将应用程序的 LauncherActivity 设为 NavigationDrawer Activity。然后我制作了两个测试片段:"VibBearFragment" 和 "FragmentTwo" 我在 onNavigationItemSelected() 中使用此代码在片段之间切换。我将 "VibBearFragment" 包含在抽屉的内容中,以便在应用程序启动时它就在那里。
Fragment fragment = new FragmentTwo();
switch (item.getItemId()) {
case R.id.nav_teddy_vib:
fragment = new VibBearFragment();
break;
case R.id.nav_fragment1:
break;
case R.id.vibActivity:
Intent i = new Intent(this, MainActivity.class);
startActivity(i);
break;
case R.id.nav_call_me:
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:0123456789"));
startActivity(callIntent);
break;
default:
fragment = new FragmentTwo();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.vibFragment, fragment).commit();
但是 "VibBearFragment" 只是掩盖了 "FragmentTwo" 并且它们 overlap.And 当 Fragments 是 overlapping.The 时所有的按钮都有效 overlapping.The 有趣的是,如果我点击 "FragmentTwo" 它们重叠,但如果我单击 "VibBearFragment",即使 "FragmentTwo" 已打开,它也能正常工作。
这是被语法替换的 XML 片段
<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.slaven.teddybear.Fragments.VibBearFragment"
android:id="@+id/vibFragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
tools:layout="@layout/vib_bear_fragment" />
我已尽力向您展示问题所在。还有按钮 N2(来自图片) 当活动重叠时消失,但仍然works.Can有人帮忙吗?
您不能使用静态片段(在 xml 布局中定义的片段)进行交易。如果您确实打算进行交易,请手动添加它们。
<RelativeLayout 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/RelativeLayoutSwap"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.slaven.teddybear.Drawer"
tools:showIn="@layout/app_bar_drawer">
<FrameLayout android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
然后在您的活动 onCreate() 方法中添加来自布局的片段:
//...onCreate()
Fragment content = getSupportFragmentManager().findFragmentById(R.id.content);
if (savedInstance == null || content == null) {
// add the initial fragment from the layout
content = new VibBearFragment();
getSupportFragmentmanager().beginTransaction().replace(R.id.content, content).commit();
}