如何水平对齐菜单项

时间:2017-07-21 05:03:57

标签: android

如何创建第一行水平对齐的菜单项,如google chrome

enter image description here

我用菜单子项创建了第一个项目,但通过这种方式我得到一行将我发送到另一个菜单,那么如何使用菜单文件呢?我不想使用Dialog

这是我的菜单文件:

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

    <group android:id="@+id/toolbar_menu"
        android:visible="true">
    <item
        android:id="@+id/item_tabs"
        android:icon="@drawable/ic_tabs_24dp"
        android:title="@null"
        app:showAsAction="always"/>
    <item
        android:id="@+id/icon_row_menu_id"
        android:title="@null"
        android:visible="true">
        <menu>
            <item
                android:id="@+id/item_back"
                android:icon="@drawable/ic_arrow_back_24dp"
                android:title="@string/go_back" />
            <item
                android:id="@+id/item_forward"
                android:icon="@drawable/ic_arrow_forward_24dp"
                android:title="@string/go_forward" />
            <item
                android:id="@+id/item_bookmark_page"
                android:icon="@drawable/ic_star_border_24dp"
                android:title="@string/bookmark_this_page" />
            <item
                android:id="@+id/item_download_page"
                android:icon="@drawable/ic_download_24dp"
                android:title="@string/download_page" />
            <item
                android:id="@+id/item_info"
                android:icon="@drawable/ic_info_outline_24dp"
                android:title="@string/view_site_information" />
        </menu>
    </item>
    <item
        android:id="@+id/item_new_tab"
        android:title="@string/new_tab" />
    <item
        android:id="@+id/item_new_incognito_tab"
        android:title="@string/new_incognito_tab" />
    <item
        android:id="@+id/item_bookmarks"
        android:title="@string/bookmarks" />
    <item
        android:id="@+id/item_history"
        android:title="@string/history" />
    <item
        android:id="@+id/item_downloads"
        android:title="@string/download" />
    <item
        android:id="@+id/item_settings"
        android:title="@string/settings" />
    </group>
</menu>

在这里我如何使用它:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mainMenu = menu;
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

2 个答案:

答案 0 :(得分:1)

您可以使用项目标记的git push origin foo-branch属性,如

app:actionLayout="@layout/layout_name"

,您的<item android:id="@+id/menu_id" android:title="@string/title" app:showAsAction="never" app:actionLayout="@layout/layout_name"/> 是任何布局文件,如

layout_name

//在此处添加水平菜单项

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

您可以访问此项

</LineearLayout>

答案 1 :(得分:0)

您也可以尝试使用PopUp窗口,这可以帮助您检查上面的代码

popup.xml布局文件不是菜单,

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:background="@color/cardview_light_background"
              android:layout_margin="20dp">

    <LinearLayout
        android:layout_margin="5dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        //your images with horizontal

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

       // your vertical list view
    </LinearLayout>
</LinearLayout>

您可以将以下代码添加到onClick()方法:

LayoutInflater layoutInflater
                        = (LayoutInflater) context
                        .getSystemService(LAYOUT_INFLATER_SERVICE);

                final View popupView = layoutInflater.inflate(R.layout.popup, null);
                final PopupWindow popupWindow = new PopupWindow(
                        popupView,
                        ActionMenuView.LayoutParams.WRAP_CONTENT,
                        ActionMenuView.LayoutParams.WRAP_CONTENT, true);

                popupWindow.setOutsideTouchable(true);
                popupWindow.setFocusable(true);


                popupWindow.setBackgroundDrawable(new BitmapDrawable());

                View parent = view.getRootView();

                popupWindow.showAtLocation(parent, Gravity.TOP|Gravity.END,0,0);