工具栏菜单无法在Android API 19

时间:2016-10-11 03:57:42

标签: android android-toolbar

我正在尝试让我的Android应用程序在多个版本上运行并且遇到了API 19的问题。我有一个不可点击的工具栏。工具栏显示但单击未触发。该应用程序适用于API 21和23.我还注意到视图寻呼机也不可点击,但滑动工作。不确定我做错了什么......

工具栏:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbarGeneral"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay" />

风格:

<resources>
  <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBarOverlay">false</item>
 </style>
 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBarOverlay">true</item>
 </style>
</resources>

MainActivity:

 public class MasterActivity : AppCompatActivity, View.IOnClickListener//, Android.Support.V4.App.FragmentActivity
{

    //private XmppClient _xmppClient;
    private ViewPager _mViewPager;
    //private bool _shouldImportContact;
    //private bool _shouldInitialiseSystem;
    private AppCompatActivity _context;
    private MasterPageChangeListener _pageChangeListener;
    private IMenuItem _editMenu;
    private SQLiteConnection _dbConnection;
    private int _fragmentPos;
    private Android.Support.V4.App.Fragment[] _masterFragments;
    private CRUDRepository<FacebookInfo> _facebookCRUD;
    private FacebookInfo _facebookInfo;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        _facebookInfo = new FacebookInfo();
        _facebookCRUD = new CRUDRepository<FacebookInfo>();
        _fragmentPos = Intent.GetIntExtra("FragmentPos", 0);
        _dbConnection = SQLiteHelper.GetConnection();
        try
        {
            _context = this;

            SetContentView(Resource.Layout.Master); //Set view the Master view

            Toolbar.Toolbar toolbar = FindViewById<Toolbar.Toolbar>(Resource.Id.toolbar);
            this.SetSupportActionBar(toolbar);


            _mViewPager = FindViewById<ViewPager>(Resource.Id.viewPager);
            _mViewPager.Adapter = new MasterAdapter(base.SupportFragmentManager, _masterFragments, titleLabels);

            _mViewPager.SetCurrentItem(_fragmentPos, true);

            TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.tabs);
            tabLayout.SetupWithViewPager(_mViewPager);

            FloatingActionButton fab = FindViewById<FloatingActionButton>(Resource.Id.fab);
            fab.SetOnClickListener(this);

            //Create the pageChangeListener class so that the page selected is known
            _pageChangeListener = new MasterPageChangeListener(this, _mViewPager);
            _mViewPager.AddOnPageChangeListener(_pageChangeListener);


        }
        catch (Exception Ex)
        {
            var data = Ex.Message;
        }

    }

OnPrepareOptionsMenu和OnCreateOptionsMenu:

 public override bool OnPrepareOptionsMenu(IMenu menu)
    {

        if (_pageChangeListener.tabPosition == 0)
            _editMenu.SetVisible(true);
        else if (_pageChangeListener.tabPosition == 1 || _pageChangeListener.tabPosition == 2)
            _editMenu.SetVisible(false);
        return base.OnPrepareOptionsMenu(menu);          
    }


    public override bool OnCreateOptionsMenu(IMenu menu)
    {
        base.OnCreateOptionsMenu(menu);
        var inflater = MenuInflater;
        inflater.Inflate(Resource.Menu.menu, menu);
        _editMenu = menu.FindItem(Resource.Id.action_add);
        return true;
    }

1 个答案:

答案 0 :(得分:0)

问题直接与此有关:

1)您需要将app:正确定义为xmlns:app="http://schemas.android.com/apk/res-auto"而不是xmlns:app="http://schemas.android.com/apk/lib-auto"

2)您需要将单词app:layout_behaviour更改为app:layout_behavior

随意使用相应的代码编辑您的原始问题,这样这个答案更有意义! (随着更多源代码在PM中显示)