Android learning notes-51 _ coding implementation software interface, write fixed interfaces to XML, and write logic changes to programs,

Source: Internet
Author: User


51 _ coding implementation software interface
--------------------------
1. in addition to using XML to implement the software interface, android can also implement the software interface by coding, and in some cases, it can only implement the software interface by coding, for example: some content needs to be displayed according to the calculation result when the software is running. If not required, we recommend that you use XML,
This makes the application comply with the MVC design pattern and has a good software hierarchy.
-----------------------------------------------------------------
2. When the style is displayed based on the calculation result, it is difficult to use XML for the interface.
------------------------------------
3. For example:
----------------------------------------
This part is often changed and can be controlled in encoding.
----------------------------------------
--------------------------------------------
This part remains unchanged and can be extracted and put into XML.
----------------------------------------------
A. If the software interface is implemented by coding, you need to put the unchanged part in XML, which makes it easier to modify the interface later.
B. But the interface performance should be better achieved through coding.
C. The software must comply with the MVC mode.
------------------------------------------------------
4. The following is the example source code of the interface implemented through encoding.
A. Create an android project: codeui
B./codeui/src/COM/credream/codeui/codeuiactivity. Java
Package com. credream. codeui;

Import Android. App. activity;
Import Android. content. context;
Import Android. OS. Bundle;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. View. viewgroup;
Import Android. widget. linearlayout;
Import Android. widget. textview;

Public class codeuiactivity extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// ①. Create a linear layout object and pass in the context object
Linearlayout = new linearlayout (this );
/* The parent class of linearlayout is viewgroup.
* Viewgroup is a view container. It is used to hold some sub-controls. It is available only when viewgroup is inherited.
* The viewgroup inherits the view function of the sub-control. In Android
* The container is inherited from the View class.
*
**
*/
// Vertical layout
Linearlayout. setorientation (linearlayout. Vertical );
// ①. 1 create a textview
Textview = new textview (this );
// Set the display content of textview
Textview. settext (R. String. Hello );
// Specify the layout parameter of textview: width or height.
// Specify the width to fill the parent element. The content of the high wrap_content package
Viewgroup. layoutparams textviewparams = new viewgroup. layoutparams (
Viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. wrap_content );
// ①. 2 Add the textview control to the linear layout. Textview layout control, global parameters of textviewparams
Linearlayout. addview (textview, textviewparams );
// ③ Get the Layout View in part. xml
View partview = getpartui ();
// ③. 1 Add the layout of part. XML to the linear layout.
Linearlayout. addview (partview );

Viewgroup. layoutparams = new viewgroup. layoutparams (
Viewgroup. layoutparams. fill_parent, viewgroup. layoutparams. fill_parent );
Setcontentview (linearlayout, layoutparams );
}
// ② Obtain the interface of a part
Private view getpartui (){
// Layoutinflater is a built-in service of the Android system.
Layoutinflater Inflater = (layoutinflater) getsystemservice (context. layout_inflater_service );
Return Inflater. Inflate (R. layout. Part, null );
// Inflater. Inflate (R. layout. Part, null); Return view,
// The first parameter is the resource ID. You can generate the corresponding view object according to the interface ID in part. xml.
// The second parameter indicates whether to link the generated view object to a parent element.
}
}
Bytes ------------------------------------------------------------------------------------
C./codeui/RES/layout/part. 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 = "wrap_content"
Android: Orientation = "vertical">
<! -- This XML is the unchanged part of the control interface. An edittext control is added here -->
<Edittext
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
/>

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "add"
/>
</Linearlayout>
------------------------------------------------
D./codeui/RES/values/strings. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>

<String name = "hello"> Hello world, codeuiactivity! </String>
<String name = "app_name"> codeui coding implementation software interface </string>

</Resources>
------------------------------------------------------------------------

 

 

Related Article

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.