简体   繁体   English

如何在主要活动中添加片段

[英]how to add fragment in main activity

I want to add a fragment called today in my main activity But im using a drawer in this activity 我想在我的主要活动中添加今天的片段,但我在此活动中使用了抽屉

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, ActionBar.TabListener{

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

        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);
        //codi

    }

Like mentioned in the comments, you should just read a tutorial about that. 就像评论中提到的那样,您应该只阅读有关此内容的教程。 Put that in the oncreate() method 把它放在oncreate()方法中

 getFragmentManager().beginTransaction()
                .add(R.id.container, yourFragment()                            
                .commit();

The first paramter of the add() method is the ViewGroup in the activities XML. add()方法的第一个参数是活动XML中的ViewGroup。 where the Fragment should be placed (R.layout.activity_main in your case). 应该放置片段的位置(在您的情况下为R.layout.activity_main)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM