简体   繁体   中英

Android User interface: activity_main.xml & content_main.xml

I am new in Android development. At the moment I am trying figure out how Android user interface is created.

I remember, previously, user interface & layout was designed in activity_main.xml files.

I have created a new activity, which generated me two files for user interface: activity_main.xml & content_main.xml . It seems, that content_main.xml contains layout and user interface elements. In this case, what activity_main.xml . Do I have to use it?

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @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();
            }
        });
    }
}

you should use it . you use include layout to keep clean and understandable your xml..most of the time your xml is to big which is very difficult to find any view(Button,Image etc) ..rather than including entire view into one xml we seprate them and include them ....when you setContentView your layout only one view hierarchy is created..

you have to use activity_main.xml because it is the root layout. As per my knowledge content_main.xml is for fab. So if you want a new button or anything you can directly add it to activity_main.xml. Many people recommend to use multiple files because now if you want same layout as content_main.xml in another layout file you can directly include that. This is the advantage which multiple file usage provides.

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