2.1.3 control the UI interface in the Code and control the UI interface with a mix of XML and Java code

Source: Internet
Author: User

Mainactivity. Java
/*** @ Author administrator * @ Param indicates that you can use code to control the layout without using XML **/public class mainactivity extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // create a linear layout manager new 1 linearlayout layout = new linearlayout (this); // set layout super for this activity. setcontentview (layout); layout. setorientation (linearlayout. vertical); // textview final textview TV = new textview (this); // button B = new button (this); B. settext (R. string. OK); B. setlayoutparams (New viewgroup. layoutparams (viewgroup. layoutparams. wrap_content, viewgroup. layoutparams. wrap_content); // Add textview layout to the layout container. addview (TV); // Add the button layout to the layout container. addview (B); // bind an event listener to the button B. setonclicklistener (New onclicklistener () {public void onclick (view v) {TV. settext ("Hello, Android," + new Java. util. date ());}});}}

 

Strings. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "hello"> Hello world, mainactivity! </String> <string name = "app_name"> test69 </string> <string name = "OK"> I changed show to TV, here, the local variable is called using final ** local variable </string> </resources>
From the rough text code of the above program, we can see that all the UI components used in this program are created by using the new keyword, and then the program uses the linearlayout container to "hold" These UI components, in this way, a T graphical user interface is formed. Running result

 


Run the above program in the simulator and you will see the interface shown in 2.4. It is not difficult to see from the above program code that completely controlling the UI interface in the Code is not only not conducive to high-level decoupling, but also because of NewCreate Ui ComponentsYou must call a method to set the behavior of the UI component. Therefore, the code is very bloated. XML file controlUI, developers only need to use tags in the XML layout file to create the UI component, and they only need to configure simple properties to control the behavior of the UI component, so it is much simpler. Tip: 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. Although Android applications allows developers to control the UI in code like developing swing applications. interface, but this method is not only cumbersome for programming, but also not conducive High-level decouplingTherefore, developers are not recommended to use this method. > 2.1.4 use the XML layout file and Java code to control the UI mixedWhen you use XML layout files and code together to control the U1 interface, you may manage components with small changes and fixed behavior in XML layout files. Components with many changes and complex behavior controlIt is managed by the Java code. Example: simple image browserFor example, in the following application, we first define a simple linear layout container in the layout file. The code of this layout file is as follows. Program list: Codes \ 02 \ 2.1 \ mix view \ res \ iayout \ main. xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:id="@+id/root"    >
</LinearLayout>
Mainactivity. Java

Public class mainactivity extends activity {// defines the int [] images = new int [] {R. drawable. mm0, R. drawable. mm01, R. drawable. mm02, R. drawable. mm03, R. drawable. mm04, R. drawable. mm05, R. drawable. mm06, R. drawable. mm07, R. drawable. mm08,}; int currentimg = 0; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // obtain the linearlayout layout container linearlayout Mian = (linearlayout) findviewbyid (R. id. root); // create the imageview component final imageview image = new imageview (this); // Add the imageview component to the linearlayout layout container mian. addview (image); // initialize and display the first image. setimageresource (images [0]); image. setonclicklistener (New onclicklistener () {public void onclick (view v) {If (currentimg >=4) {currentimg =-1;} // change the image displayed in imageview. setimageresource (images [++ currentimg]) ;}}

Run the following command:

The first line of the Code in the above program obtains the linearlayou "linear layout manager" displayed by the activity ). Lines 2nd and 3rd in boldCreates an imageview and adds the imageview to-the linearlayout layout manager is managed through the XML layout file, while the imageview component is managed by the Java code. In addition, the above program also adds a click event for the imageview component. When you click this component, imageview displays the next image. Run the above program and you can see the page shown in 2.5.

 

  

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.