Android uses XML file layout to modify HelloWorld program _android

Source: Internet
Author: User

Android environment is finished, directly is a HelloWorld program, please see "Using Adt-bundle easy to build the Android development environment and Hello World (Linux)" which makes it difficult for many people to clarify the basic structure of the entire Android project. In fact, the Android project, with other Java project projects, SSH, servlet, and so on, are taking Java and XML files in a joint way to form a project.

There are several documents that need to be known. The following figure:


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

Where the Setcontentview (R.layout.activity_main) in the protected void OnCreate (Bundle savedinstancestate) {} method is specified, To load the Activity_main.xml layout file under the Res file. I can do this. is because it has a Gen folder that eclipse will automatically update. The R.java file for the Gen folder will be updated with your XML update.


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


We're going to change the activity_main.xml under the res file. The following figure:


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 the code to do so, and finally, as shown in the previous illustration:

<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 attributes mean that the size of the label label's control occupies the entire screen.

In this way, you can use the android:gravity= "center_vertical" property to center the screen, where Android4.0 development, layout_gravity seems to have no use, directly with the gravity attribute.

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

So we're going to change the Strings.xml file under the Values folder. Find the Name=hello_world string 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" Han this program.


You can see in the activity_main.xml that the TextView component's style is in the strings.xml specified Text_style, then we change the Styles.xml, and then add a Text_style entry, the following code, Make the hellowssssssssssssssssorld! string bold and red, as the CSS file in the Web development strings.xml. The only thing to note here is that the units in the Andong are SP, not px, which is good for your Android program to run on each major device.

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

When you are done, the following illustration shows:


This will result in the following:


The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.