实现在Android的一个菜单项内部三项行动三项、菜单项、行动、Android

2023-09-12 02:47:18 作者:待我强大给我媳妇天下

我希望有相似的菜单项的功能中的铬浏览器的移动,因为它是在图片。我想有后退,前进,刷新在单行菜单项。我如何能实现类似的菜单项?有没有任何参考或有任何黑客把这个功能?

我的应用程序仅适用于平板电脑的目的。这是我目前的操作栏菜单项:

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
<项目
    机器人:ID =@ + ID /最爱
    机器人:showAsAction =总是
    机器人:标题=快乐/>
<项目
    机器人:ID =@ + ID / share_button
    机器人:图标=@可绘制/股
    机器人:showAsAction =总是
    机器人:标题=分享/>
<项目
    机器人:ID =@ + ID / hola_button
    机器人:showAsAction =总是
    机器人:标题=霍拉/>
<项目
    机器人:图标=@可绘制/ more_actions
    机器人:showAsAction =总是>
    <菜单>
        <项目
            机器人:ID =@ + ID / back_button
            机器人:图标=@可绘制/后退
            机器人:标题=后面的/>
        <项目
            机器人:ID =@ + ID / forward_button
            机器人:图标=@可绘制/前进
            机器人:标题=前进/>
        <项目
            机器人:ID =@ + ID / refresh_button
            机器人:图标=@可绘制/刷新
            机器人:标题=刷新/>
    < /菜单>
 < /项目>
 < /菜单>
 

解决方案

编辑:

这个例子是作为OverflowMenu(因为ABS抛弃溢出主题)。你可以膨胀任何类型的布局组合。此类从PopUpWindow延伸并且不使用典型onCreateOptions。它采用了ABS-CustomView和PopUpWindow创建菜单的。

Android Q露出庐山真面目

这是Android的参考:一个弹出窗口,可以用来显示任意的观点。弹出窗口是在当前活动的顶部显示一个浮动的容器。

布局类似于您要求的布局。这种观点是固定在动作条,但你可以显示任何你想要的。弹出窗口支持多种显示功能开箱即用。

定制OverflowMenu

 公共类OverflowMenu扩展PopupWindow {

私人查看mContentView;

公共OverflowMenu(上下文的背景下,INT RESOURCEID){
    超(上下文);
    mContentView = LayoutInflater.from(上下文).inflate(RESOURCEID,NULL);
    的setContentView(mContentView);
    自动调用setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

    //处理touchevents
    setOutsideTouchable(真正的);
    setFocusable(真正的);
    setBackgroundDrawable(新BitmapDrawable());
}

/ **
 *附加​​的OverflowMenu查看到ActionBar的右上角
 * @参数actionBarView
 * /
公共无效展(查看actionBarView){
    INT X = actionBarView.getMeasuredWidth() -  mContentView.getMeasuredWidth();
    showAsDropDown(actionBarView中,x,0);
}

/ **
 *返回mContentView,
 *用于mContentView.findViewById();
 * @返回
 * /
公共查看getView(){
    返回mContentView;
}
}
 

MainActivity

 公共类MainActivity扩展SherlockActivity {

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_mai N);

    最后的动作条动作条= getSupportActionBar();
    actionBar.setCustomView(R.layout.actionbar);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    最后OverflowMenu菜单=新OverflowMenu(这一点,R.layout.menu_overflow);

    最后的ImageButton overflowBtn =(的ImageButton)actionBar.getCustomView()findViewById(R.id.actionbar_overflow)。
    overflowBtn.setOnClickListener(新OnClickListener(){

        @覆盖
        公共无效的onClick(视图v){
            menu.show(actionBar.getCustomView());
        }
    });
  }


 }
 

I want to have similar menu item functionality as in the chrome browser for mobile as it is in the picture. I want to have back, forward, refresh menu items in a single row. How can I implement a similar menu item? is there any reference or is there is any hack to bring this functionality?

My app is aimed only for tablets. Here is my current Action bar menu item:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/favorite"
    android:showAsAction="always"
    android:title="Happy"/>
<item
    android:id="@+id/share_button"
    android:icon="@drawable/share"
    android:showAsAction="always"
    android:title="Share"/>
<item
    android:id="@+id/hola_button"          
    android:showAsAction="always"
    android:title="Hola"/>
<item
    android:icon="@drawable/more_actions"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/back_button"
            android:icon="@drawable/back"
            android:title="back"/>
        <item
            android:id="@+id/forward_button"
            android:icon="@drawable/forward"
            android:title="forward"/>
        <item
            android:id="@+id/refresh_button"
            android:icon="@drawable/refresh"
            android:title="refresh"/>          
    </menu>
 </item>
 </menu>

解决方案

EDIT:

This examples is as an OverflowMenu (since ABS ditched the overflow-theme). you can inflate any kind of layout-combinations. This class extends from PopUpWindow and doesn't use the typical onCreateOptions. It uses the ABS-CustomView and a PopUpWindow to create menu's.

From android reference: A popup window that can be used to display an arbitrary view. The popup window is a floating container that appears on top of the current activity.

The layout looks similar to your requested layout. This view is anchored to the ActionBar but you can display it anywhere you want. Popup window supports many show functions out of the box.

Customizable OverflowMenu

public class OverflowMenu extends PopupWindow {

private View mContentView;

public OverflowMenu(Context context, int resourceId) {
    super(context);
    mContentView = LayoutInflater.from(context).inflate(resourceId, null);
    setContentView(mContentView);
    setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    setWidth(WindowManager.LayoutParams.WRAP_CONTENT);

    // Handle touchevents
    setOutsideTouchable(true);
    setFocusable(true);
    setBackgroundDrawable(new BitmapDrawable());
}

/**
 * Attach the OverflowMenu View to the ActionBar's Right corner
 * @param actionBarView
 */
public void show(View actionBarView) {
    int x = actionBarView.getMeasuredWidth() - mContentView.getMeasuredWidth();
    showAsDropDown(actionBarView, x, 0);
}

/**
 * Return mContentView, 
 *  used for mContentView.findViewById();
 * @return
 */
public View getView(){
    return mContentView;
}
}

MainActivity

public class MainActivity extends SherlockActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mai n);

    final ActionBar actionBar = getSupportActionBar();
    actionBar.setCustomView(R.layout.actionbar);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

    final OverflowMenu menu = new OverflowMenu(this,R.layout.menu_overflow);

    final ImageButton overflowBtn = (ImageButton) actionBar.getCustomView().findViewById(R.id.actionbar_overflow);
    overflowBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            menu.show(actionBar.getCustomView());
        }
    });
  }


 }