我想加载片段布局按钮点击活动。当我这样做时,它成功地重定向到片段,但问题是片段布局与活动布局重叠。请帮帮我。
这是我的活动布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/linearLayout7"
android:layout_alignTop="@+id/linearLayout6"
android:layout_toEndOf="@+id/linearLayout6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView14"
android:layout_marginLeft="25dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView15"
android:layout_marginLeft="200dp" />
</LinearLayout>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
这是我的碎片布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textSize="40dp"
android:textStyle="bold"
android:id="@+id/Add"
android:layout_centerInParent="true"/>
ThreeFragment扩展了Fragment,在Activity内部按钮中单击这就是我加载Fragment的方式。没有出现错误,唯一的问题是视图重叠。
AddPlan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new FourFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
});
尝试将背景放在片段上:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
background="#FFFFFF"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"
android:textSize="40dp"
android:textStyle="bold"
android:id="@+id/Add"
android:layout_centerInParent="true"/>
或在添加新片段之前清除其他片段:
fragmentManager.popBackStack();
用以下代码替换您的代码:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new FourFragment();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="@+id/linearLayout7"
android:layout_alignTop="@+id/linearLayout6"
android:layout_toEndOf="@+id/linearLayout6">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView14"
android:layout_marginLeft="25dp"
android:layout_marginTop="1dp"
android:layout_marginRight="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView15"
android:layout_marginLeft="200dp" />
</LinearLayout>
</RelativeLayout>
如果您想让片段完全隐藏活动布局,可以考虑使用DialogFragment。
还可以查看优秀的悬崖笔记,了解如何使对话框全屏显示。
它基本上包括以下步骤:
- 从DialogFragment派生片段类
- 将活动中的片段显示为对话框
- 确保对话框片段的样式为全屏