简体   繁体   English

如何在工具栏中创建按钮覆盖 Android?

[英]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.您可以通过在menu目录中创建一个菜单来简单地做到这一点。 Then add that menu to the toolbar using OnCreateOptionMenu and control using OnOptionItemSelectedListener .然后使用OnCreateOptionMenu将该菜单添加到工具栏并使用OnOptionItemSelectedListener进行控制。

In the activity's onCreate method, set the toolbar with在活动的 onCreate 方法中,将工具栏设置为

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信用:我们如何将菜单设置为工具栏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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