Android implementation options menu submenu

Android options menu and submenu learning notes for your reference. The specific contents are as follows

Menu introduction:

Menus are widely used in desktop applications. Almost all desktop applications have menus. However, with the development of mobile phones, the use of mobile phone desktop menus has been greatly reduced. Generally speaking, we put menus into applications. The difference between desktop application menus and mobile application menus is that desktop menus are generally visible, while mobile phones are not visible. Usually, users need to press the menu key on the mobile phone to pop up menu related applications, Here, the simple use of the next dish is explained through a small case.

Generally, there are two ways to create menus: one is in Java code and the other is in XML. This time, we mainly explain the cases of creating menus in XML, because XML is generally recommended to create menus. Compared with java code, it is easier to maintain, has strong scalability and low coupling.

Case implementation: when users input text, they can select font size, font color, etc. through the menu

Implementation steps:

1. First, create the menu XML under the menu of the resource folder

menu_ main.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
 <item
  android:id="@+id/menuFont"
  android:title="字体大小">
  <menu>
   <group
    android:id="@+id/groupFont"
    android:checkableBehavior="none">
    <item
     android:id="@+id/font_10"
     android:title="10" />
    <item
     android:id="@+id/font_12"
     android:title="12" />
    <item
     android:id="@+id/font_14"
     android:title="14" />
    <item
     android:id="@+id/font_16"
     android:title="16" />
    <item
     android:id="@+id/font_18"
     android:title="18" />
   </group>
  </menu>
 </item>

 <item
  android:id="@+id/menuColor"
  android:icon="@mipmap/ic_launcher"
  android:title="字体颜色">
  <menu>
   <group
    android:id="@+id/groupColor"
    android:checkableBehavior="none">
    <item
     android:id="@+id/red"
     android:title="红色" />
    <item
     android:id="@+id/blue"
     android:title="蓝色" />
    <item
     android:id="@+id/green"
     android:title="绿色" />
   </group>
  </menu>
 </item>
</menu>

For the above XML file:

'<'item... / > element: defines the menu item.' < ' Group... / > sub element: package multiple menu items defined by '<'item... / > into a menu group. The' <'gruop... / > sub element is used to control the behavior of the whole group of menus. This element can specify the following common attributes:

Generally, the '<'item... / > element is used to define menu items, and the' <'item... / > element can contain the '<'Menu... / > element. The' <'Menu... / > inside the '<'item... / > element is its sub menu.

For the '<'item... / > element, you can specify the following common attributes:

Android: ID: specify a unique ID for the menu item Android: Title: specify the title of the menu item Android: icon: specify the icon of the menu item Android: alphabeticshortcut: specify a character shortcut key for the menu item Android: numericshortcut: specify a numeric shortcut key for the menu item Android: checkable: set whether the item is optional Android: checked: set whether the menu item is selected Select Android: visible: to set whether the menu item is visible. Android: enable: to set whether the menu item is available

2. Set the activity associated with the menu item, and override oncreateoptions menu (menu menu) in the activity. If you need to listen to the event of the menu item, you need to override it

onOptionsItemSelected(MenuItem item)

MainActivity.java

public class MainActivity extends AppCompatActivity {

private EditText mEdt;
private TextView mTv;

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mEdt = (EditText) findViewById(R.id.mEdt);
 mTv = (TextView) findViewById(R.id.mInput);
}

/**
 * 通过该方法添加菜单项或子菜单项
 * @param menu
 * @return
 */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
 //方式一添加菜单布局
 MenuInflater inflater = new MenuInflater(this);
 //将解析到的布局添加到menu中
 inflater.inflate(R.menu.menu_main,menu);
 //方式二添加菜单布局
// getMenuInflater().inflate(R.menu.menu_main,menu);
 return super.onCreateOptionsMenu(menu);
}

/**
 * 设置菜单项的点击事件
 * @param item
 * @return
 */
@Override
public boolean onOptionsItemSelected(MenuItem item) {

 switch (item.getItemId()) {
  case R.id.menuColor:
   Toast.makeText(this,"你选择了修改颜色",Toast.LENGTH_SHORT).show();
   break;
  case R.id.menuFont:
   Toast.makeText(this,"你选择了修改字体",Toast.LENGTH_SHORT).show();
   break;
 }
 return super.onOptionsItemSelected(item);
 }
}

That's the basic layout. Look at the overall layout

Submenu under font size:

Submenu under font color:

3. Start to implement the function. Here I only post the method of function implementation: options itemselected (MenuItem item)

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 //先判断点击的是哪个id
 switch (item.getItemId()) {
  case R.id.font_10:
   mEdt.setTextSize(10 * 2);
   break;
  case R.id.font_12:
   mEdt.setTextSize(12 * 2);
   break;
  case R.id.font_14:
   mEdt.setTextSize(14 * 2);
   break;
  case R.id.font_16:
   mEdt.setTextSize(16 * 2);
   break;
  case R.id.font_18:
   mEdt.setTextSize(18 * 2);
   break;
  case R.id.blue:
   mEdt.setTextColor(Color.BLUE);
   break;
  case R.id.red:
   mEdt.setTextColor(Color.RED);
   break;
  case R.id.green:
   mEdt.setTextColor(Color.GREEN);
   break;
 }
 return super.onOptionsItemSelected(item);
}

Operation results:

That's all for the simple menu layout. There is another method for the menu, that is, the context menu. For the context menu, I'll introduce it next time<

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>