超简单的ActionBar

来源:互联网 发布:缠通套利指标源码破解 编辑:程序博客网 时间:2024/04/28 16:28

首先需要一张图片icon,这里使用的图片名称是iconn

第一步:在layout中新建一个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"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.popupwindowoverflow.MainActivity">
    <item
        android:id="@+id/action_new"
        android:orderInCategory="1"
        android:title="SubMenu"
        android:icon="@drawable/iconn"
        app:showAsAction="always">
        <menu>
            <item android:id="@+id/submenu1"
               android:title="Accept"
               android:titleCondensed="Accept"
               android:icon="@drawable/square_blue" />
            <item android:id="@+id/submenu2"
               android:title="Unread"  
               android:titleCondensed="Unread"
               android:icon="@drawable/square_grey" />
       </menu>
    </item>

</menu>

第二步:实现点击每个子item显示提示信息,在 public boolean onOptionsItemSelected(MenuItem item){}函数里添加如下代码

    @Override
       public boolean onOptionsItemSelected(MenuItem item) {
           // Handle action bar item clicks here. The action bar will
           // automatically handle clicks on the Home/Up button, so long
           // as you specify a parent activity in AndroidManifest.xml.
           int id = item.getItemId();
           switch (item.getItemId()) {
           case R.id.submenu1:
               Toast.makeText(this, "Compose", Toast.LENGTH_SHORT).show();
               return true;
           case R.id.submenu2:
               Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
               return true;
           default:
               return super.onOptionsItemSelected(item);
           }
       }

0 0
原创粉丝点击