【问题标题】:Custom actionbar layout with overflow menu带有溢出菜单的自定义操作栏布局
【发布时间】:2014-08-09 15:26:36
【问题描述】:

我使用带有自定义操作栏的actionbarsherklock 库,如下所示:

我的自定义工具:

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

这是自定义布局:

<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" />

问题是我想添加一个溢出菜单来分享这样的按钮:

请告诉我如何使用自定义操作栏布局来做到这一点。

【问题讨论】:

  • 您的设备有硬件菜单按钮吗?
  • 我支持 android 2.3 的许多设备,所以我认为是的。
  • android-developers.blogspot.in/2012/01/…。这可能会有所帮助
  • @Raghunandan 对不起,我不明白你的意思。我再说一遍:我支持许多带或不带菜单按钮的设备。当用户按下菜单按钮时,我不在乎。我只想在他们按下共享按钮时显示溢出菜单。这有意义吗?
  • 如果有帮助,请检查blog.vogella.com/2013/08/06/…

标签: android android-actionbar actionbarsherlock


【解决方案1】:

似乎没有right 解决这个问题的方法,所以我尝试了一个hack,使用带有列表适配器的popup windows 来伪造操作栏溢出菜单。
看起来像这个例子:http://rajeshandroiddeveloper.blogspot.com/2013/07/android-popupwindow-example-in-listview.html

【讨论】:

    【解决方案2】:

    只需覆盖这两种方法即可在您的活动中调用溢出菜单,其中一种是扩展 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);
    }
    

    【讨论】:

    • 你不明白这一点。因为我使用自己的自定义操作栏布局,所以我不能像上图那样将菜单添加到共享按钮中。
    • @R4j 我还使用了我的自定义操作栏并打开了与上图相同的下拉菜单,所以用不同的解决方案回答您自己的问题,并使其成为正确的解决方案不是正确的方法!!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    相关资源
    最近更新 更多