About Android (9): 10. Using XML files and Java code to control the UI interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.