简体   繁体   English

具有溢出菜单的自定义操作栏布局

[英]Custom actionbar layout with overflow menu

I use the actionbarsherklock library with custom action bar look like this: 我使用带有自定义操作栏的actionbarsherklock库,如下所示:

在此输入图像描述

My custom implement: 我的自定义工具:

  ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    // Do any other config to the action bar
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // set custom view
    View actionBarView = getLayoutInflater().inflate(
            R.layout.action_bar_default, null);

    View btnMenuLeft= actionBarView.findViewById(R.id.btnMenuLeft);
    btnMenuLeft.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            toggle();
        }
    });

    View btnMenuShare= actionBarView.findViewById(R.id.btnMenuShare);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    actionBar.setCustomView(actionBarView, params);

    // Hide the home icon
    actionBar.setIcon(android.R.color.transparent);
    actionBar.setLogo(android.R.color.transparent);

And here is the custom layout: 这是自定义布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/nav_bar_bg"
android:gravity="center"
android:orientation="horizontal" >

<!-- menu button -->
    <ImageButton
        android:id="@+id/btnMenuLeft"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/list_btn"
        android:clickable="false"
        android:duplicateParentState="true"
        android:focusable="false" />

    <!-- logo -->
    <ImageView       
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="1"
    android:src="@drawable/app_logo" />

    <!-- share button -->
    <ImageButton
         android:id="@+id/btnMenuShare"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/action_btn"
        android:clickable="false"
        android:duplicateParentState="true"
        android:focusable="false" />

The problem is that I want add an over flow menu to share button like this one: 问题是我想添加一个溢流菜单来分享像这样的按钮:

在此输入图像描述

Please tell me how can I do that with the custom action bar layout. 请告诉我如何使用自定义操作栏布局执行此操作。

Seem there is no right way to solve this problem, so I tried a hack by using popup windows with list adapter to fake action bar overflow menu. 似乎没有right方法来解决这个问题,所以我尝试使用带有列表适配器的popup windows来假冒操作栏溢出菜单。
It look like this example: http://rajeshandroiddeveloper.blogspot.com/2013/07/android-popupwindow-example-in-listview.html 它看起来像这样的例子: http//rajeshandroiddeveloper.blogspot.com/2013/07/android-popupwindow-example-in-listview.html

Just override this two methods to call overflow menu in your activity which one is extending ActionBarActivity : 只需覆盖这两个方法来调用活动中的溢出菜单,其中一个是扩展ActionBarActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.your_menu, menu);
    return true;
}

@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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

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