简体   繁体   English

工具栏菜单不适用于ViewPager

[英]Toolbar Menu doesn't work with ViewPager

I'm learning android and currently trying to add toolbar from android.support:design to my app, but also I want to flip pages,after several research about this question in web, I write such code 我正在学习android并且目前正在尝试将android.support:design工具栏添加到我的应用程序,但我也想翻页,经过几次关于web的这个问题的研究,我写了这样的代码

1 ) activity_main.xml 1)activity_main.xml

<RelativeLayout
android:id="@+id/main_layout"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<include
    layout="@layout/toolbar"/>
<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="0dp"
    style="@style/MyCustomTabLayout"
    android:background="@drawable/bottom_menu"
    android:layout_alignParentBottom="true"
    android:paddingTop="?attr/actionBarSize"
     />

<com.example.misha.myapplication.utils.CustomViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/tab_layout"/>
</RelativeLayout>

2) toolbar.xml 2)toolbar.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_toolbar"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@color/primary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
>
</android.support.v7.widget.Toolbar>

Next, in java code I have written code in MainActivity.java 接下来,在java代码中,我在MainActivity.java中编写了代码

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);
    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_home_on));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_my_favorit_off));
    tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.button_such_on));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setBackgroundResource(R.drawable.button_background_off);
    viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount(), getBaseContext());
    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
                         }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}
 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
   return true;
 }

4 and my menu_main.xml 4和我的menu_main.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/action_search"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="ifRoom"
    android:orderInCategory="200"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>

<item
    android:id="@+id/action_user"
    android:orderInCategory="300"
    android:title="User"
    android:icon="@drawable/no_name_"
    app:showAsAction="ifRoom"/>
</menu>

Besides, in values/styles.xml 此外,在values / styles.xml中

  <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>

</style>

In AndroidManifest.xml 在AndroidManifest.xml中

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"

And I have trouble - when I click on toolbar menu nothing is happening... 我遇到了麻烦 - 当我点击工具栏菜单时,没有发生任何事情......

I try to replace in main_menu.xml 我尝试在main_menu.xml中替换

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
    android:orderInCategory="100" app:showAsAction="never" />
</menu>

But here is the same effect... Could you help me please, where is my error ? 但是这里有同样的效果......你能帮帮我吗,我的错误在哪里?

UPDATE When I have trying to solve the problem, I noticed, that if I remove 更新当我试图解决问题时,我注意到,如果我删除

viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter
        (getSupportFragmentManager(), tabLayout.getTabCount(),     getBaseContext());
    viewPager.setAdapter(adapter);
   tabLayout.setupWithViewPager(viewPager);
   viewPager.addOnPageChangeListener(new     TabLayout.TabLayoutOnPageChangeListener(tabLayout));

menu will be work. 菜单会有效。

I have forgotten to say that I am using smartphone and tablet with API - 17 for test , and another smartphone with api 15. But testing on emulator with api 23 - all good work. 我忘了说我正在使用带有API-17的智能手机和平板电脑进行测试,另一款带有api 15的智能手机。但是使用api 23在模拟器上进行测试 - 一切都很好。

And I have trouble - when I click on toolbar menu nothing is happening 而且我遇到了麻烦 - 当我点击工具栏菜单时,没有任何事情发生

Honestly, Sir, nothing should happen, because you have not implemented 老实说,先生,没有任何事情应该发生,因为你没有实施

@Override
public boolean onOptionsItemSelected(MenuItem item) {
  // this is where you handle menu clicks etc etc.
  // so add a switch statement and handle clicks
  switch (item.getItemId()) {
case R.id.id:

    break;

default:
    break;
}
return true;
}

also add this to your manifest 也将此添加到您的清单中

application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.NoActionBar" >//this guy

Please change @style/AppTheme.PopupOverlay with @style/ThemeOverlay.AppCompat.ActionBar in activity_main.xml. 请使用activity_main.xml中的@ style / ThemeOverlay.AppCompat.ActionBar更改@ style / AppTheme.PopupOverlay。 It works for me. 这个对我有用。

<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
            android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary" app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM