如何在android的菜单项中添加切换按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21282279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:17:31  来源:igfitidea点击:

How to add toggle button in menu item in android

androidmenuandroid-optionsmenu

提问by Anju

I have options menu item in my application. Requirement was to add a toggle button to a menu item. Is this possible?

我的应用程序中有选项菜单项。要求是向菜单项添加切换按钮。这可能吗?

回答by ozbek

As of this writing there are 3 options.

在撰写本文时,有3 个选项

1)Use app:actionViewClass. Example:

1)使用app:actionViewClass。例子:

<?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:title="Switch!"
        app:actionViewClass="android.widget.Switch"
        app:showAsAction="always" />
</menu>

2)You can use a custom layout in a menu item to add toggle button. Example:

2)您可以在菜单项中使用自定义布局来添加切换按钮。例子:

Create a layout with Switch(alternatively, you may also use ToggleButton), res/layout/menu_switch.xml:

创建一个布局Switch(或者,您也可以使用ToggleButtonres/layout/menu_switch.xml,:

<?xml version="1.0" encoding="utf-8"?>
<Switch xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:padding="64dp" />

And use that layout in menu item:

并在菜单项中使用该布局:

<?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:title="@string/switch_button_title"
        app:actionLayout="@layout/menu_switch"
        app:showAsAction="always" />
</menu>

3)You need to set android:checkableproperty of the menu to trueand control its checked state in runtime. Example:

3)您需要在运行时android:checkable将菜单的属性设置为true并控制其选中状态。例子:

Menu:

菜单:

<item
    android:id="@+id/checkable_menu"
    android:checkable="true"
    android:title="@string/checkable" />

Activity:

活动

private boolean isChecked = false;

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem checkable = menu.findItem(R.id.checkable_menu);
    checkable.setChecked(isChecked);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.checkable_menu:
            isChecked = !item.isChecked();
            item.setChecked(isChecked);
            return true;
        default:
            return false;
    }
}

Hope this helps.

希望这可以帮助。

回答by GreatC

use app:actionViewClass

使用 app:actionViewClass

 <item android:id="@+id/id"
    android:title="@string/string"
    app:actionViewClass="android.widget.ToggleButton"
    android:orderInCategory="80"
    app:showAsAction="always" />

回答by Digvesh Patel

public boolean onPrepareOptionsMenu(final Menu menu) {       
      if(super.mMapView.isTraffic()) 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_off_48); 
      else 
           menu.findItem(MENU_TRAFFIC_ID).setIcon(R.drawable.traffic_on_48); 

      return super.onPrepareOptionsMenu(menu); 
 }

回答by Pararth

Do you mean you want to add a toggle button as one of the elements/items appearing in the options menu or add a button to a list item from the menu?

您的意思是要添加一个切换按钮作为选项菜单中出现的元素/项目之一,还是要向菜单中的列表项添加按钮?

Then you can do it with a custom layout(use a ListViewwithin if you want) and inflating it in the

然后您可以使用自定义布局(ListView如果需要,请使用内部)并在

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

and you can save the values each time the button is toggles.

并且您可以在每次切换按钮时保存这些值。

public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
    case R.id.btnToggleValue:
      // save it here
      return true;
    case R.id.btnSecond:
      ...
      return true;
    default:
      return super.onOptionsItemSelected(item);
  }
}