Android - 片段在Activity Foreground上弹出

时间:2017-08-23 08:59:30

标签: android android-fragments

为什么会这样?

Image

单击RelativeLayout时,片段应该开始,但不是这样。我做错了什么附:我是Fragments的新手

源代码:DrawerActivity.java:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_del_h);

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.sss);
    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            FragmentManager fragmentManager = DelHome.this.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Fragment fragment = new LocationPicker();
            fragmentTransaction.add(R.id.fragment_container, fragment);
            fragmentTransaction.commit();

        }
    });

Fragment.xml代码:

<FrameLayout 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="datasite.com.aroba.LocationPicker">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_blank_fragment"
    android:textColor="@color/black"
    android:textSize="20sp"/>

DrawerActivity.xml:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragment_container">
..........
.......
.......
</FrameLayout>

2 个答案:

答案 0 :(得分:0)

您在目前的基础上添加了新的FragmentFragment没有背景,所以你可以看透它。

您可以为Fragment添加背景,或者您可能想要replace片段代替?

fragmentTransaction.replace(R.id.fragment_container, fragment);

对于布局,这就是我对NavDrawer所拥有的。

这是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:itemTextColor="@color/white_alpha"
        app:menu="@menu/activity_main_drawer" />


</android.support.v4.widget.DrawerLayout>

这是我的app_bar_main.xml,其中包含我正在替换的容器。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appBarLayout"/>

</RelativeLayout>

然后当我将一个项目替换到容器中时,它会保留Toolbar及其所在的所有内容。

答案 1 :(得分:0)

您的MainActivity的Insde添加片段,然后用另一个片段替换当前片段,这将是您做您想做的事情的更好方式

你可以在这里找到演示:http://chrisrisner.com/Using-Fragments-with-the-Navigation-Drawer-Activity