"Android" modifies the HelloWorld program with an XML file layout

Source: Internet
Author: User

Android environment is finished, directly is a HelloWorld program, see "Android" with Adt-bundle under Linux easy to build Android development environment and Hello World (click the Open link), This makes it difficult for many people to sort out the basic structure of an entire Android project. In fact, the Android project, and other Java projects, SSH, servlet, etc., are to take the Java and XML files in a joint way to form a project.

There are several files that need to be well known. Such as:


Mainactivity.java is the gateway to the entire Android project,

wherein, inside the protected void OnCreate (Bundle savedinstancestate) {} Method of Setcontentview (R.layout.activity_main); The layout file to load the Activity_main.xml under the res file. This can be done. is because it has a Gen folder that eclipse will automatically update. The Gen folder's R.java file will be updated with your XML update.


So, after that, we modify the HelloWorld program into a red string centered vertically across the screen, as shown in the following:


We are going to change the activity_main.xml under the res file. Such as:


There was originally a TextView component, something similar to the label text of the vb,c++,c# label component. Very normal, because the original HelloWorld program is to have a HelloWorld text in the upper left corner of the apk.

We modify its code so that it ends up as shown:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <textview        android:gravity=" center_vertical "        android:layout_width=" Fill_ Parent "        android:layout_height=" fill_parent "        android:text=" @string/hello_world "         style=" @style/ Text_style "        /></relativelayout>
Its android:layout_width= "fill_parent", android:layout_height= "fill_parent" two properties means that the size of the label's control is occupied by the entire screen.

In this way, in order to android:gravity= the "center_vertical" property, so that it is centered in the screen, here Android4.0 development, Layout_gravity seems to have been no longer used, directly with the Gravity property.

The text of this component is then written in the Hello_world tag of the String.xml, where the corresponding value can also be spelled out, but Eclipse warns because, as struts2 advocates, everything is labeled for later internationalization. There's really no need to do that in struts2. In Android, it makes sense to adapt at least to each screen.

So we'll go on to modify the Strings.xml file under the Values folder. Find this string of name=hello_world and change it to something you want to change. Of course, here you can also change the title of the program App_name, the HelloWorld changed to "Hello, the World" in the Han of this program.


Can be seen in the Activity_main.xml, the style of this TextView component in the Strings.xml specified Text_style item, after we change the styles.xml, and then add a Text_style entry, the following code, Make the hellowssssssssssssssssorld! string bold and red, this strings.xml as a CSS file in Web development. The only notable thing here is that the wirelessly unit is SP, not px, which is good for your Android program running on all major devices.

    <style name= "Text_style" >        <item name= "android:textsize" >24sp</item>        <item name= " Android:textcolor "> #ff0000 </item>        <item name=" Android:textstyle ">bold</item>    </ Style>

As shown in the following:


This gives you the following effect:


"Android" modifies the HelloWorld program with an XML file layout

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.