简体   繁体   中英

How to create button overlay in Toolbar Android?

I want to create a menu item in this Toolbar view, but I didn't use what to create it? 在此处输入图像描述

SOLVE SELF:

 <com.google.android.material.button.MaterialButton
                             android:id="@+id/image_quote"
                             android:layout_width="30dp"
                             android:layout_height="30dp"
                             app:icon="@drawable/ic_fi_br_quote_right"
                             app:iconTint="@color/colorBlack"
                             app:iconSize="13dp"
                             app:cornerRadius="15dp"
                             android:layout_margin="3dp"
                             android:insetLeft="0dp"
                             android:insetTop="0dp"
                             android:insetRight="0dp"
                             android:insetBottom="0dp"
                             app:iconGravity="textStart"
                             app:iconPadding="0dp"
                             style="@style/ShapeB" />

You can simply do that just by creating a menu in the menu directory. Then add that menu to the toolbar using OnCreateOptionMenu and control using OnOptionItemSelectedListener .

In the activity's onCreate method, set the toolbar with

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

Then add this method.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.yourmenufile, menu);
  return super.onCreateOptionsMenu(menu);
}

Then add menu click listener,

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case 0:
            // do whatever
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Credit: How can we set menu to toolbar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM