View menu items on the toolbar

Can I view the toolbar in the menu items?

I need to see how it is shown in yellow between two menu items on the toolbar.

+3
source share
2 answers

Just create a separator view in the xml file to display the separator line.

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>

and set actionLayoutto xml menu item to show dividing line

activity_menu.xml

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

android: actionLayout = "@ layout / dividerLine" shows the dividing line between the menu items. Hope this helps you.

Update

I get this view from my code above:

enter image description here

+1
source

custom layout menuItem uing actionLayout, oncreateOptionsMenue menuItem

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

xml

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

onCreateOptionsMenu

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        LinearLayout actionItemLayout = (LinearLayout) menu.findItem(R.id.item_save).getActionView();
        ImageView iv = (ImageView)actionItemLayout.findViewById(R.id.actionItemImage);
        iv.setBackgroundDrawable(getResources().getDrawable(R.drawable.icon_save));
}

, .

0

Source: https://habr.com/ru/post/1624760/


All Articles