The menu of the Android mobile phone is elegant and elegant, and its programming implementation is concise and bright. If anyone has used QT interface programming, then the android menu programming will smile immediately,
Option menu:
Take a look at the following code:
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. Menu;
Import Android. View. menuitem;
Public class helloactivity extends activity {
@ Override
Public Boolean oncreateoptionsmenu (menu ){
// Todo auto-generated method stub
Super. oncreateoptionsmenu (menu );
Menu. Add (0, menu. First + 1, 1, R. String. menu_open );
Menu. Add (0, menu. First + 2, 2, R. String. menu_edit );
Menu. Add (0, menu. First + 3, 3, R. String. menu_update );
Menu. Add (0, menu. First + 4, 4, R. String. menu_close );
Return true;
}
@ Override
Public Boolean onoptionsitemselected (menuitem item ){
// Todo auto-generated method stub
Super. onoptionsitemselected (item );
Switch (item. getitemid ())
{
Case menu. First + 1:
{
This. settitle ("Open Text! ");
Break;
}
Case menu. First + 2:
{
This. settitle ("edit text! ");
Break;
}
Case menu. First + 3:
{
This. settitle ("update text! ");
Break;
}
Case menu. First + 4:
{
This. settitle ("Close text! ");
Break;
}
}
Return true;
}
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
This. setcontentview (R. layout. Main );
}
}
You only need to rewrite the two functions of the parent activity, which is very convenient. That is to say, Android has already written the relevant code for you. You only need to fill in some menu items, and then write a specific
The operation code is enough.
Android Resources
There are several lines in the above Code that may be hard to understand
1. Add menu. Add (0, menu. First + 1, 1, R. String. menu_open );
Menu. Add (0, menu. First + 2, 2, R. String. menu_edit); R. String. menu_open/menu_edit in the code
These actually refer to the IDs in the resource file, which are mapped to specific resources. Here they are mapped to string resources menu_open and menu_edit. For specific values, see Res/values/string. XML defines the string value here:
<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, helloactivity! </String>
<String name = "app_name"> helloworld </string>
<String name = "menu_open"> open </string>
<String name = "menu_edit"> edit </string>
<String name = "menu_update"> Update </string>
<String name = "menu_close"> close </string>
</Resources>
In Android, the layout displayed by the activity can also be defined in the resource, and the corresponding XML file can be operated visually. It can be seen that Res/layout/Main. XML is a layout file, which specifies the interface elements in the layout and how to organize, relative location, absolute location, and other information. Let's take a look at the content:
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview Android: Id = "@ + ID/textview01" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "mytest OK YEST! "> </Textview>
</Linearlayout>
The layout is linearlayout, which contains a textview. The value of textview is mytest. after the XML file is compiled, you can use R. layout. the main ID to obtain from the resource.
Therefore, the activity can use setcontentview (R. layout. Main) to directly obtain the layout from the resource and draw interface elements.
Another common resource is image.
There are some images under Res/drawable/. You can also add some new images here. Then, you can obtain the corresponding image from the resource by using the ID of. R. drawable. xxx.
Context Menu:
The above is the menu at the bottom of the mobile phone. Android also provides context menu operations, which is quite simple. See the following code: