在我的片段中还显示了Mainactivity的布局

时间:2017-07-20 07:15:28

标签: java android xml android-layout android-fragments

我第一次使用Fragments工作,很抱歉,如果我做的非常愚蠢:D

我有导航抽屉活动。当我在第一个项目上单击它时,我想打开一个片段。当我打开它时,不仅显示了片段的布局,而且还有我的MainActivity的布局,它看起来很奇怪。

  [1]: https://i.stack.imgur.com/iqTAq.png MainActivity

  [2]: https://i.stack.imgur.com/kULQY.png Navigation Drawer

  [3]: https://i.stack.imgur.com/T2vDJ.png First Fragment

我该如何解决这个问题?

如果您需要,我的代码如下:

MainActivity(导航抽屉模板化,我只在onNavigationItemSelected中编写,所以当我点击第一个项目时它会转到片段:

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            setTitle("First Fragment");
            first first = new first();
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment, first).commit();
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

content_main(它应该属于MainActivity)

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.gsr.fragmenttest.MainActivity"
    tools:showIn="@layout/app_bar_main">

   <FrameLayout
       android:id="@+id/fragment"
       android:layout_width="match_parent"
       android:layout_height="match_parent">


      <TextView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:text="This is the Mainactivity, no fragemnt"/>

   </FrameLayout>

</RelativeLayout>

第一片段活动

public class first extends Fragment {

    Button btn;
    TextView textView;

    public first() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_first, container, false);
        btn=(Button)v.findViewById(R.id.button);
        textView=(TextView)v.findViewById(R.id.textView);


        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    textView.setText("Nice, it works !!");
            }
        });
        // Inflate the layout for this fragment
        return v;
    }

}

至少是片段布局文件

<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="com.gsr.fragmenttest.first">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">



        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Hello Fragment"
            android:textSize="50dp"/>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"/>


    </RelativeLayout>

</FrameLayout>

我希望有人可以帮助我:)。

3 个答案:

答案 0 :(得分:1)

在片段布局中添加背景,它可以解决您的问题。

<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"
    android:background="@color/white"                 //Your background color
    tools:context="com.gsr.fragmenttest.first">

    <!-- TODO: Update blank fragment layout -->

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        ...

答案 1 :(得分:0)

我认为问题在于你的活动布局

<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" >


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

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

尝试像上面一样更改主要活动布局。希望它能解决你的问题。

答案 2 :(得分:0)

1)实现first.OnOnFragmentInteractionListener

2)在onNavigationItemSelected方法中放入此代码

 if (id == R.id.nav_camera) {
        setTitle("First Fragment");
        first first = new first();
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.fragment, new first()).commit();

3)在第一个片段中使用此行

import android.app.Fragment;