Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well.
--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.
** (1) xml description **
--android: id: id of this choice --android: title: Display string --app: showAsAction: Whether to show in the action bar
** (2) Submenu **
In the menu xml file, describe the combination of menu-item tags in the item tag. Submenus can be displayed by nesting.
(3)showAsAction
The app: showAsAction attribute of the item tag has the following three values.
--never: Overflow menu without displaying in action bar Store in --always: Always show in action bar (not recommended) --ifRoom: Display only when the action bar has room
(Caution)
If the parent class of the activity class is not AppCompatAction but just Activity, it will be the android: showAsAction attribute.
** (4) icon **
You can specify the menu icon with the android: icon attribute of the item tag. However, the options with this attribute have the following specifications.
--The icon is not displayed in the overflow menu. --Only the icon is displayed in the action bar --If you add "| withText" to the showAsAction attribute, the menu name will also be displayed depending on the terminal size.
(Caution)
You can make your own icon image, but the Android SDK also has a convenient one. To use them, specify "@android: drawable / ic_menu _...".
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menuReset"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
android:title="@string/menu_reset"
app:showAsAction="always|withText"/>
<item
android:id="@+id/menuFonttype"
android:title="@string/menu_fonttype"
app:showAsAction="never"
>
<menu>
<item
android:id="@+id/menuFonttypeSerif"
android:title="@string/menu_fonttype_serif"
app:showAsAction="never"/>
<item
android:id="@+id/menuFonttypeSunserif"
android:title="@string/meun_fonttype_sunsserif"
app:showAsAction="never"/>
<item
android:id="@+id/menuFonttypeMonospace"
android:title="@string/menu_fontttype_monospace"
app:showAsAction="never"/>
</menu>
</item>
<item
android:id="@+id/menuFontstyle"
android:title="@string/menu_fontstyle"
app:showAsAction="never"
>
<menu>
<item
android:id="@+id/menuFontstyleNormal"
android:title="@string/menu_fontstyle_normal"
app:showAsAction="never"/>
<item
android:id="@+id/menuFontstyleItalic"
android:title="@string/menu_fontstyle_italic"
app:showAsAction="never"/>
<item
android:id="@+id/menuFontstyleBold"
android:title="@string/menu_fontstyle_bold"
app:showAsAction="never"/>
<item
android:id="@+id/menuFontstyleBoldItalic"
android:title="@string/menu_fontstyle_bolditalic"
app:showAsAction="never"/>
</menu>
</item>
</menu>
To display the option menu described in the xml file, in the activity class You need to write the ** onCreateOptionsMenu () ** method.
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(Option menu xml file name, menu);
return true;
}
The process when the option menu is selected is described in the ** onCreateOptionsMenu () ** method. The argument item (Menu item type) is processed by the following pattern using this argument item for one selected menu.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case R.id.Button R value 1:
//Process 1
break;
case R.id.Button R value 2:
//Process 2
break;
...
...
}
TIPS
Describe the following in the activity class.
android.support.v7.app.ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
public boolean onOptionsItemSelected(MenuItem item) { Describe the following in the switch statement of.
case android.R.id.home:
finish();
db.close();
break;
With the above procedure, the following option menu will appear in the upper right corner. When you tap, ... If you tap "Font" again, ... When you tap "Font"
See here for a list of icons.
that's all. It was how to put out the menu bar on Android. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.
Recommended Posts