Android recommends using an XML file to set the UI interface and then control the logical part with Java Code, which shows MVC thought.
The full name of MVC is the model view Controller, which is models -Views (view) -Controller (Controller) abbreviation, a software design paradigm that organizes code with a separate approach to business logic, data, and interfaces, and aggregates business logic into a single component, without the need to rewrite business logic while improving and customizing the interface and user interaction.
We can define an XML filein the app\src\main\res\layout directory (ther.java file will automatically ingest the resource), You can then pass the Setcontentview in the Activity component of the Java code (r.layout.< Resource file name >) is displayed.
When you add a component to an XML file, you can specify the android:id property for the component , and then you can pass in the Java code Findviewbyid (R.id.<android:id properties >) accesses the component.
Of course, Android also supports the full use of Java code to set up the UI interface, similar to Swing , here is a simple example:
Package Zxc.blogtest;import Android.os.bundle;import Android.support.v7.app.actionbaractivity;import Android.widget.button;import Android.widget.linearlayout;import Android.widget.textview;public class MainActivity Extends Actionbaractivity { @Override protected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); LinearLayout layout = new LinearLayout (this); Super.setcontentview (layout); Layout.setorientation (linearlayout.vertical); Final TextView show = new TextView (this); Button bn = New button (this); Show.settext ("HelloWorld"); Bn.settext ("button"); Layout.addview (show); Layout.addview (BN); }}
The This that is passed in when the component is created is a context parameter, which is the base class for activity and service, and the passed parameters allow the UI component to get the global variables to Android.
The way the UI interface is set up is not very graceful, and it is confusing and highly recommended to put together other logic code.
About the XML file, there are already a simple introduction, there will be a considerable number of blog to introduce this aspect of the content, here is not to say.
About Android (9): 10. Using XML files and Java code to control the UI interface