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

时间:2015-12-29 17:20:46

标签: java android xml

enter image description here(here is a pic of my toolbar with the menu items)

我有一个包含4个菜单项的工具栏,但我无法找到在每个项目之间添加divderlines的方法。 Heres是工具栏的.xml文件

EDITED

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

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

}`

任何帮助都会非常感谢!

2 个答案:

答案 0 :(得分:1)

在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>

答案 1 :(得分:0)

就快速Bing搜索而言:

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2014 The Android Open Source Project
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
 
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <size android:height="2dip"/>
    <solid android:color="#ffffffff"/>
</shape>

Android XML Divider

相关问题