简体   繁体   中英

Android Studio 1.4 - Explain activity_main.xml and content_main.xml

I just updated my Android Studio to 1.4, and I'm a bit confused about activity_main.xml and the other xml file content_main.xml.

I have this app that programatically generates xml. Everything is fine, except I don't have the action bar.

My onCreate is like this:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    ...
   // set layout programatically


  setContentView(myCustomXML);

How do I get the action bar? There must be some very simple solution to this.

Many design components that were introduced with Android Lollipop version. The main problem you are probably not awared right now is the Toolbar Widget .

As you can see in your AndroidManifest.xml , for MainActivity there is "...NoActionBar" theme used. Therefor there is no default actionbar placed on top od activity_main layout. Instead of that there is android.support.v7.widget.Toolbar component used and it is part of content XML.

Notice, that Toolbar is part of activity_main.xml and probably not part of your MyCustomXML .

If this is not your problem, then you probably just need to setSupportActionBar(android.support.v7.widget.Toolbar) or setActionBar(android.widget.Toolbar) .

After you have set action bar from your layout.xml you can get it via getSupportActionBar() / getActionBar() methods.

Hopefuly, this is what you are looking for, because your question is not 100% clear

Edit: Do not use setContentView() twice and do not expect anything to work if you work with views before setContentView() method is called (Toolbar is also view)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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