编程语言
首页 > 编程语言> > java – 工具栏菜单不适用于ViewPager

java – 工具栏菜单不适用于ViewPager

作者:互联网

我正在学习android并且目前正在尝试将android.support:design中的工具栏添加到我的应用程序,但我也想翻页,经过几次关于web的这个问题的研究,我写了这样的代码

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

<?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>

接下来,在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和我的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>

此外,在values / styles.xml中

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

</style>

在AndroidManifest.xml中

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

我遇到了麻烦 – 当我点击工具栏菜单时,没有发生任何事情……

我尝试在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>

但是这里有同样的效果……你能帮帮我吗,我的错误在哪里?

UPDATE
当我试图解决问题时,我注意到,如果我删除

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));

菜单会有效.

我忘了说我正在使用带有API-17的智能手机和平板电脑进行测试,另一款带有api 15的智能手机.但是使用api 23在模拟器上进行测试 – 一切都很好.

解决方法:

And I have trouble – when I click on toolbar menu nothing is happening

老实说,先生,没有任何事情应该发生,因为你没有实施

@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;
}

也将此添加到您的清单中

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

标签:java,android,android-toolbar,android-menu
来源: https://codeday.me/bug/20190702/1358144.html