(original) "Android Programming Authority Guide" learning note 01--Android app first experience--005

Source: Internet
Author: User

From layout XML to view object

  While creating the Geoquiz project, an activity subclass named Quizactivity was also created. The Quizactivity class file is stored in the SRC directory of the project. directory src is the repository of all Java source code for the project.

  Open the Quizactivity.java file to see the code in it, as follows:

  

 PackageCom.bignerdranch.android.geoquiz;ImportAndroid.os.Bundle;Importandroid.app.Activity;ImportAndroid.view.Menu; Public classQuizactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_quiz); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.quiz, menu); return true; }    }

Default class file for code listing 1-4:quizactivity (Quizactivity.java)

The Java class file consists of two activity methods: OnCreate (Bundle) and Oncreateoptionsmenu (Menu).
Ignore the Oncreateoptionsmenu method, which is described in detail in the 16th chapter.
  once an instance of the activity subclass is created, the OnCreate (Bundle) method is called. Once the activity is created, it needs to acquire and manage its own user interface. Gets the activity's user interface, which invokes the following activity methods:

 Public void int layoutresid)

By passing in the resource ID parameter of the layout, the method generates a view of the specified layout and places it on the screen. When layout view is generated, the components that the layout file contains are also instantiated with their own property definitions.

Resource and Resource ID

A layout is a resource . Resources are content that applies to non-code situations, such as files, audio files, and XML files.

  all resource files for the project are stored in a subdirectory of the directory res . From the package browser you can see that the layout activity_quiz.xml resource files are stored in the res/layout/directory. The strings file containing the string resource is stored in the res/values/directory.

Expand Directory Gen in the package browser, locate and open the R.java file to see the current resource ID of the Geoquiz app. R.java files are automatically generated during the Android project compilation process, follow the warning in the header of the file, and do not attempt to modify the contents of the file.

/*auto-generated FILE. Do not MODIFY.  * * This class is automatically generated by the * AAPT tool from the resource data it found. It * should not being modified by hand. */ PackageCom.bignerdranch.android.geoquiz; Public Final classR { Public Static Final classattr {} Public Static Final classDimen {/**Default screen margins, per the Android Design guidelines. Customize dimensions originally defined in res/values/dimens.xml (such as screens margins) for SW720DP devices (e.g .             "Tablets" in landscape. */         Public Static Final intactivity_horizontal_margin=0x7f040000;  Public Static Final intactivity_vertical_margin=0x7f040001; }     Public Static Final classdrawable { Public Static Final intic_launcher=0x7f020000; }     Public Static Final classID { Public Static Final intaction_settings=0x7f080000; }     Public Static Final classLayout { Public Static Final intactivity_quiz=0x7f030000; }     Public Static Final classMenu { Public Static Final intquiz=0x7f070000; }     Public Static Final classString { Public Static Final intaction_settings=0x7f050004;  Public Static Final intapp_name=0x7f050000;  Public Static Final intfalse_button=0x7f050003;  Public Static Final intquestion_text=0x7f050001;  Public Static Final inttrue_button=0x7f050002; }    ...}

Code Listing 1-5:geoquiz Applying the current resource ID (R.java)

So far, we haven't referenced strings in our code, but if you want, you should use the following methods:

Settitle (R.string.app_name);

To generate a resource ID for a component, add the Android:id property to the component when you define it. In the Activity_quiz.xml file, add the Android:id property for each of the two buttons, as shown in code listing 1-6.

<linearlayout xmlns:android = "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:gravity= "Center"android:orientation= "Vertical" > <TextView android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:padding= "24DP"Android:text= "@string/question_text"/> <LinearLayout android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:orientation= "Horizontal" > <Button android:id = "@+id/true_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/true_button"/> <Button android:id = "@+id/false_button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/false_button"/> </LinearLayout> </LinearLayout>

Code Listing 1-6: Adding a Resource ID for a button (Activity_quiz.xml)

Note that the Android: id attribute value is preceded by a + flag , and the Android:text property value is not , because we are going to create the resource ID, and the string resource is simply a reference.

  Save the Activity_quiz.xml file, re-view the R.java file, and confirm that there are two new resource IDs generated in the r.id inner class, as shown in Listing 1-7.

 Public Final classR {... Public Static Final classID { Public Static Final intaction_settings=0x7f080002;  Public Static Final intfalse_button=0x7f080001;  Public Static Final inttrue_button=0x7f080000; }...

Code Listing 1-7: New resource ID (R.java)

(original) "Android Programming Authority Guide" learning note 01--Android app first experience--005

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.