由于invalidateOptionsMenu(),Android切换按钮在操作栏中不起作用

时间:2018-11-26 18:12:13

标签: android menu android-actionbar switchcompat

我正在使用操作栏上的菜单。我的菜单包含两项(位置符号和切换按钮)。当gps状态更改时,我的位置图标也会更改。如果我不调用invalidateOptionsMenu(),我的切换按钮将起作用,否则它将起作用。我希望在我调用invalidateOptionsMenu()时切换按钮能够正常工作。我该怎么办?

菜单:

<item
    android:id="@+id/myGps_image"
    android:title=""
    app:showAsAction="always"
    android:icon="@drawable/ic_location_off"/>
<item
    android:id="@+id/myswitch"
    android:title=""
    app:showAsAction="always"
    android:actionLayout="@layout/switch_layout"
    />

switch_layout:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.SwitchCompat
    android:id="@+id/switchForActionBar"
    android:layout_width="?attr/actionBarSize"
    android:layout_height="?attr/actionBarSize"
    android:gravity="center_vertical"
    android:theme="@style/SwithButtonMf"/>

onCreateOptionsMenu:

    @Override
public void onCreateOptionsMenu(final Menu menu, MenuInflater inflater) {
    Objects.requireNonNull(getActivity()).invalidateOptionsMenu();
    inflater.inflate(R.menu.switch_menu,menu);
    MenuItem item=menu.findItem(R.id.myswitch);

    item.setActionView(R.layout.switch_layout);
    switchCompat= menu.findItem(R.id.myswitch).getActionView().findViewById(R.id.switchForActionBar);



    MenuItem item_loc=menu.findItem(R.id.myGps_image);
    item_loc.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            // menu.getItem(0).setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_location_on));

            if(!is_gps_enable){
                android.app.AlertDialog.Builder builder=new android.app.AlertDialog.Builder(getActivity(),R.style.AlertDialogCustom);
                builder.setTitle(R.string.open_gps);
                builder.setCancelable(false);
                builder.setIcon(R.drawable.ic_location_red);

                builder.setMessage(R.string.gps_mode_high_accuracy);
                builder.setPositiveButton(getActivity().getString(R.string.ayarlar), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        getActivity().startActivity(intent);
                        dialogInterface.dismiss();
                    }
                });

                builder.setNegativeButton(getActivity().getString(R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });

                builder.create().show();

            }else {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                getActivity().startActivity(intent);
            }


            return false;
        }
    });

    if(item_loc!=null){
        item_loc.setIcon(is_gps_enable?R.drawable.ic_location_on:R.drawable.ic_location_off);
    }



    super.onCreateOptionsMenu(menu, inflater);

}

0 个答案:

没有答案