弹出菜单从操作栏中的图标展开/折叠

2024-03-13

我正在开发 Android 2.1 API 7 应用程序。我用以下方法实现我的操作栏操作栏夏洛克 http://actionbarsherlock.com/图书馆。

我的操作栏视图:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

      <ImageView
          android:id="@+id/my_option"
          android:layout_gravity="left"  
          android:src="@drawable/ic_launcher"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
       />
</LinearLayout>

在我的活动中onCreate()打回来:

 @Override
    protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);

        ActionBar actionBar = getSupportActionBar();
        View actionBarView = getLayoutInflater().inflate(R.layout.action_bar, null);

        actionBar.setCustomView(actionBarView);

        ImageView actionBarImg = (ImageView) actionBarView.findViewById(R.id.my_option);
        actionBarImg.setOnClickListener(new OnClickListener(){

              public void onClick(View view) {
                  //how to pop up a menu which is expand/collapse below the image icon                         
              }
        });
   }

如您在上面看到的,有一个图像图标(actionBarImg)在操作栏布局上。

我想实现这样的功能,当用户手指按下图像图标时,弹出菜单将在图标下方展开,当再次按下图标时,弹出菜单会折叠。如何实施?


添加变量:

private PopupWindow mPopupMenu;
private View mMenuLayout;
private boolean isPopupOpened = false;

添加到 onCreate():

mMenuLayout = getLayoutInflater().inflate(R.layout.menu_layout, null);
mPopupMenu = new PopupWindow(mMenuLayout, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);

actionBarImg.setOnClickListener(new OnClickListener() {

    public void onClick(View view) {
        if (isPopupOpened)
        {
            mPopupMenu.dismiss();
            isPopupOpened = false;
        }
        else
        {
            mPopupMenu.showAsDropDown(actionBarImg);
            isPopupOpened = true;
        }                        
    }

});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

弹出菜单从操作栏中的图标展开/折叠 的相关文章

随机推荐