Android 在工具栏中的菜单项之间查看

Android 在工具栏中的菜单项之间查看,android,android-layout,Android,Android Layout,是否可以在工具栏的菜单项中查看 我希望在工具栏的两个菜单项之间以黄色显示视图。您必须为menuItemuingactionLayout创建自定义布局,然后在oncreateoptions menue中获取菜单项视图,并将图像视图图标更改为垂直可绘制 布局\u actionitem.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="ma

是否可以在工具栏的菜单项中查看


我希望在工具栏的两个菜单项之间以黄色显示视图。

您必须为
menuItem
uing
actionLayout
创建
自定义布局,然后在
oncreateoptions menue
中获取菜单项视图,并将图像视图图标更改为垂直可绘制

布局\u actionitem.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView
        android:id="@+id/actionItemImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="1dp"/>

</LinearLayout>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/item_save"
        android:icon="@drawable/exit"
        android:showAsAction="always"
        android:actionLayout="@layout/layout_actionitem"
        android:title=""/>
</menu>
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/your_icon1"
      android:icon="@drawable/image1"
      android:title="@string/text1"
      app:showAsAction="always"
      />

<item android:id="@+id/icon2TricktoShowDividerLine"
     android:actionLayout="@layout/dividerLine" 
     app:showAsAction="always"
     android:title="@string/text2" />

<item android:id="@+id/your_icon3"
      android:icon="@drawable/icon"
      android:title="@string/text3"
      app:showAsAction="always" />
 </menu>

希望这有帮助。

只需在xml文件中创建分隔线视图,以显示分隔线

dividerLine.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<View
android:layout_width="1dp"
android:layout_height="?android:attr/actionBarSize" 
android:background="@android:color/darker_gray"/> 
 </LinearLayout>
android:actionLayout=“@layout/dividerLine”显示菜单项之间的分隔线。希望这对你有帮助

已更新

我从上面的代码中获得这种类型的视图:


这是可能的,但您必须忽略此视图的单击事件。简而言之,您有一个分隔器视图,但实际上它是一个
menuitem
,但单击按钮时,您必须忽略其单击行为使用属性
app:showAsAction=“always”的3个菜单项
和第二个菜单项drawable source将是分割线图像。您是否尝试过它,因为它在中不可见toolbar@Salmankhan是的,我试过了,效果很好,试着在
dividerLine.xml
中更改视图的背景色,使其可见我已经用不同的颜色试过了,但根本不可见。向我展示您的演示或图像,该工具栏带有分隔器视图。@Salmankhan查看我在回答中更新的屏幕截图需要使用app:actionLayout而不是android:actionLayout。但无论如何,谢谢你的帮助。
 <menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item android:id="@+id/your_icon1"
      android:icon="@drawable/image1"
      android:title="@string/text1"
      app:showAsAction="always"
      />

<item android:id="@+id/icon2TricktoShowDividerLine"
     android:actionLayout="@layout/dividerLine" 
     app:showAsAction="always"
     android:title="@string/text2" />

<item android:id="@+id/your_icon3"
      android:icon="@drawable/icon"
      android:title="@string/text3"
      app:showAsAction="always" />
 </menu>