How to set an icon at the end of Toolbar

后端 未结 5 1450
醉酒成梦
醉酒成梦 2021-01-17 08:36

I want to set an icon at the the end of my Toolbar,which start another activity. My Toolbar portion

 

        
相关标签:
5条回答
  • 2021-01-17 08:58

    If there's anyone that still confused (including me before), somehow android studio doesn't provide autocomplete for the android:layout_gravity under the toolbar tag. So to make it works, just copy and paste android:layout_gravity="end" to the ImageView/layout.

    0 讨论(0)
  • 2021-01-17 09:06

    If you want something like this (icon 2) example

    You do not need add icon to the layout manually, you should to implement menu.xml

    1. Create menu.xml like this

      <?xml version="1.0" encoding="utf-8"?>
      <menu xmlns:android="http://schemas.android.com/apk/res/android">
          <item android:id="@+id/new_activity"
              android:icon="@drawable/ic_custom_icon"
              android:title="@string/new_activity" />
      </menu>
      
    2. Add it in your activity/fragment to the actionbar/toolbar by

      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.menu, menu);
         return true;
      }
      
    3. Handle menu items events

    Toolbar with menus works fine, it sets icons to the right of toolbar automatically

    0 讨论(0)
  • 2021-01-17 09:10

    <item
        android:title="kjljk"
        app:showAsAction="always"
        android:id="@+id/menuitem_search"
        android:icon="@drawable/pdficon"
        >
    </item>
    

    Then in OnCreateView: setHasOptionsMenu(true); Closing the OncreateView below write this: @Override

    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.pdf, menu);
        ...
    }
    
    0 讨论(0)
  • 2021-01-17 09:15

    Try adding this to your ImageView:

    android:layout_gravity="end"
    
    0 讨论(0)
  • 2021-01-17 09:18

    Setting the orderInCategory will set the order of the menu item

     <item 
        ...
          android:orderInCategory="150"
          app:showAsAction="always"
        ..
        />
    

    if you want to put margin between your menu items you can add an empty menu item in between

    0 讨论(0)
提交回复
热议问题