效果图如下:
ToolBar的简单实用(点击弹窗实现页面切换)
这个是点击 弹窗中的1。
ToolBar的简单实用(点击弹窗实现页面切换)
这个是点击弹窗中的2。

实现代码如下:

1.依赖

implementation 'com.android.support:appcompat-v7:25.3.1'

2.xml控件(布局)

<android.support.v7.widget.Toolbar
        android:id="@+id/toobar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/ToolbarPopupTheme"
        app:titleTextColor="@color/white"
        ></android.support.v7.widget.Toolbar>

3.activity

ToolBar toobar = findViewById(R.id.toobar);//控件id
toobar.setTitle("title");//设置标题
        toobar.setSubtitle("sub");//设置子标题
        toobar.setLogo(R.mipmap.ic_launcher);//设置logo
        setSupportActionBar(toobar);//必须要写的,顶部显示toobar
        toobar.setNavigationIcon(R.drawable.ic_format_list_bulleted_white_24dp);//设置弹窗的图片点击
        //toolbar的弹窗菜单点击事件
        toobar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()){
                    case R.id.action_item1://这个是点击弹窗1
                        Log.i("TAST==","1");
                        myviewPager.setCurrentItem(0);//viewPager的页面切换,0为fragmentOne,1为fragmentTwo
                        break;
                    case R.id.action_item2://这个是点击弹窗2
                        Log.i("TAST==","2");
                        myviewPager.setCurrentItem(1);
                        break;
                }
                return true;//默认false,记得改成true
            }

        });
        //重写这个方法,实现弹窗的布局
         @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.base_toobar_menu,menu);
        return true;
    }

新建一个layout/menu/.base_toobar_menu.xml。

	<item
        android:id="@+id/action_item1"
        android:title="@string/menu_item1"
        app:showAsAction="never"/>
    <item
        android:id="@+id/action_item2"
        android:title="@string/menu_item2"
        app:showAsAction="never"/>

需要改动的模块
layout/values/styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">//改为NoActionBar
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorSecondary">@color/white</item>//添加一个白色
    </style>
//新写一个style,设置name和parent。----这是弹窗的样式
    <style name="ToolbarPopupTheme" parent="@style/ThemeOverlay.AppCompat.Dark"
        >
        <item name="android:colorBackground">@color/white</item>
        <item name="android:textColor">@color/black</item>
        <item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item>
    </style>
//新写一个style。设置name和parent,上一行会用到
    <style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
        <item name="overlapAnchor">false</item>
    </style>
</resources>

layout/values/strings.xml

<resources>
    <string name="app_name">day06test_</string>
    <string name="menu_item1">1</string>//1 2 3是弹窗的条目文字
    <string name="menu_item2">2</string>
    <string name="menu_notification">3</string>
</resources>

layout/values/color.xml

<resources>
    <color name="colorPrimary">#008577</color>
    <color name="colorPrimaryDark">#00574B</color>
    <color name="colorAccent">#D81B60</color>
    <color name="white">#ffff</color>
    <color name="black">#000</color>
</resources>

如果要实现弹窗的点击事件,使用viewPager比较简单好用。

myviewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                if(i == 0){
                    return new FragmentOne();
                }else{
                    return new FragmentTwo();
                }
            }

            @Override
            public int getCount() {
                return 2;
            }
        });

相关文章: