Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在工具栏上的每个菜单项之间添加分隔符_Java_Android_Xml - Fatal编程技术网

Java 如何在工具栏上的每个菜单项之间添加分隔符

Java 如何在工具栏上的每个菜单项之间添加分隔符,java,android,xml,Java,Android,Xml,我有一个带有4个菜单项的工具栏,但我想不出在每个菜单项之间添加divderline的方法。这是工具栏的my.xml文件 已编辑 这就是我到目前为止所做的,它仍然没有创建分隔符 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.

我有一个带有4个菜单项的工具栏,但我想不出在每个菜单项之间添加divderline的方法。这是工具栏的my.xml文件

已编辑

这就是我到目前为止所做的,它仍然没有创建分隔符

    <menu xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.mycompany.materialtest.MainActivity">

    <group android:id="@+id/homegroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/home"
            android:title="home"
            android:orderInCategory="1"
            android:icon="@drawable/homeclicked"
            app:showAsAction="always">
        </item>
    </group>
    <group android:id="@+id/searchgroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/search"
            android:title="search"
            android:orderInCategory="2"
            android:icon="@drawable/search"
            app:showAsAction="always">
        </item>
    </group>
    <group android:id="@+id/chatgroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/chat"
            android:title="chat"
            android:orderInCategory="3"
            android:icon="@drawable/chat"
            app:showAsAction="always">
        </item>
    </group>
    <group android:id="@+id/profilegroup"
        android:checkableBehavior="single">
        <item
            android:id="@+id/profile"
            android:title="profile"
            android:orderInCategory="4"
            android:icon="@drawable/user"
            app:showAsAction="always">
        </item>
    </group>
</menu> 

我还分离了mainactivity.java上的菜单项。这里是代码,如果这在任何情况下都有帮助

public void setupEvenlyDistributedToolbar(Toolbar toolbar) {
    // Use Display metrics to get Screen Dimensions
    Display display = getWindowManager().getDefaultDisplay();
    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);


    toolbar.inflateMenu(R.menu.menu_bottombar);


    // Add 10 spacing on either side of the toolbar
    toolbar.setContentInsetsAbsolute(10, 10);

    // Get the ChildCount of your Toolbar, this should only be 1
    int childCount = toolbar.getChildCount();
    // Get the Screen Width in pixels
    int screenWidth = metrics.widthPixels;

    // Create the Toolbar Params based on the screenWidth
    Toolbar.LayoutParams toolbarParams = new Toolbar.LayoutParams(screenWidth, Toolbar.LayoutParams.WRAP_CONTENT);

    // Loop through the child Items
    for (int i = 0; i < childCount; i++) {
        // Get the item at the current index
        View childView = toolbar.getChildAt(i);
        // If its a ViewGroup
        if (childView instanceof ViewGroup) {
            // Set its layout params
            childView.setLayoutParams(toolbarParams);
            // Get the child count of this view group, and compute the item widths based on this count & screen size
            int innerChildCount = ((ViewGroup) childView).getChildCount();
            int itemWidth = (screenWidth / innerChildCount);
            // Create layout params for the ActionMenuView
            ActionMenuView.LayoutParams params = new ActionMenuView.LayoutParams(itemWidth, Toolbar.LayoutParams.WRAP_CONTENT);
            // Loop through the children
            for (int j = 0; j < innerChildCount; j++) {
                View grandChild = ((ViewGroup) childView).getChildAt(j);
                if (grandChild instanceof ActionMenuItemView) {
                    // set the layout parameters on each View
                    grandChild.setLayoutParams(params);
                }
            }
        }
    }
}
public void设置均匀分布工具栏(工具栏){
//使用显示度量获取屏幕尺寸
Display Display=getWindowManager().getDefaultDisplay();
DisplayMetrics=新的DisplayMetrics();
display.getMetrics(metrics);
工具栏.充气菜单(右菜单.菜单\底部栏);
//在工具栏两侧添加10个间距
工具栏.setContentInsertsabsolute(10,10);
//获取工具栏的ChildCount,该值应仅为1
int childCount=toolbar.getChildCount();
//以像素为单位获取屏幕宽度
int screenWidth=metrics.widthPixels;
//根据屏幕宽度创建工具栏参数
Toolbar.LayoutParams toolbarams=新建Toolbar.LayoutParams(屏幕宽度、Toolbar.LayoutParams.WRAP_内容);
//循环遍历子项
for(int i=0;i
}`

任何帮助都很好,谢谢

在XML中试试这个

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:id="@+id/home"
        android:checkableBehavior="single">
        <item
        android:id="@+id/homegroup"
        android:title="@string/next"
        android:orderInCategory="1"
        android:icon="@drawable/homeclicked"
        app:showAsAction="always">
        </item>
    </group>
    <group android:id="@+id/searchgroup"
        android:checkableBehavior="single">
        <item
         android:id="@+id/search"
         android:title="@string/next"
         android:orderInCategory="2"
         android:icon="@drawable/search"
         app:showAsAction="always">
       </item>
    </group>
    <group android:id="@+id/chatgroup"
        android:checkableBehavior="single">
        <item
         android:id="@+id/chat"
         android:title="@string/next"
         android:orderInCategory="3"
         android:icon="@drawable/chat"
         app:showAsAction="always">
       </item>
    </group>
    <group android:id="@+id/profilegroup"
        android:checkableBehavior="single">
        <item
         android:id="@+id/profile"
         android:title="@string/next"
         android:orderInCategory="4"
         android:icon="@drawable/profile"
         app:showAsAction="always">
       </item>
    </group>
</menu>


谢谢Rahul的帮助,但这对我不起作用。提前谢谢@拉胡昌拉尼