Android如何在Actionbar溢出中添加切换小部件?

时间:2016-04-26 09:13:58

标签: android android-studio

我需要在操作栏菜单中添加切换小部件。切换小部件正确显示在操作栏中。但它在动作栏溢出时不可见。有人可以帮我找一个解决方案吗?

这是我的menu.xml

<?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/resp"
    app:actionViewClass="android.widget.Switch"
    app:showAsAction="ifRoom"
    android:title="Respiration" />
<item
    android:id="@+id/plotI"
    app:actionViewClass="android.widget.Switch"
    app:showAsAction="ifRoom"
    android:title="Plot I" />
<item
    android:id="@+id/plotII"
    app:actionViewClass="android.widget.Switch"
    app:showAsAction="ifRoom"
    android:title="plot II" />
<item
    android:id="@+id/plotIII"
    app:actionViewClass="android.widget.Switch"
    app:showAsAction="ifRoom"
    android:title="Plot III" />
<item
    android:id="@+id/plotIV"
    app:actionViewClass="android.widget.Switch"
    app:showAsAction="ifRoom"
    android:title="Plot IV" />
</menu>

这是我的onCreateOptionsMenu ..

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    final MenuItem plot1 = menu.findItem(R.id.plotI);
    final Switch actionView1 = (Switch) MenuItemCompat.getActionView(plot1);
    actionView1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //
        }
    });
    final MenuItem plot2 = menu.findItem(R.id.plotII);
    final Switch actionView2 = (Switch) MenuItemCompat.getActionView(plot2);
    actionView2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Start or stop your Service
        }
    });
    final MenuItem plot3 = menu.findItem(R.id.plotI);
    final Switch actionView3 = (Switch) MenuItemCompat.getActionView(plot3);
    actionView3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Start or stop your Service
        }
    });
    final MenuItem plot4 = menu.findItem(R.id.plotI);
    final Switch actionView4 = (Switch) MenuItemCompat.getActionView(plot4);
    actionView4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Start or stop your Service
        }
    });

    return super.onCreateOptionsMenu(menu);
}

0 个答案:

没有答案