Java 工具栏无法完美显示图标

Java 工具栏无法完美显示图标,java,android,Java,Android,我试图将图像添加到工具栏中,但它并没有以完美的顺序显示。它以以下方式出现: 我想要这个文本视图,其中“2”写在钟形图标的上方,钟形图标在右侧,因为我还有工具栏的标题,但没有显示出来 以下是我的代码: activity_main.xml: <androidx.appcompat.widget.Toolbar android:layout_width="match_parent" android:layout_height="wr

我试图将图像添加到工具栏中,但它并没有以完美的顺序显示。它以以下方式出现:

我想要这个文本视图,其中“2”写在钟形图标的上方,钟形图标在右侧,因为我还有工具栏的标题,但没有显示出来

以下是我的代码:

activity_main.xml:

<androidx.appcompat.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/notification_layout"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="Helping Hands"
        app:titleMarginStart="40dp"/>

菜单\u action\u bar.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_hotlist"
        app:actionLayout="@layout/action_bar_notifitcation_icon"
        android:icon="@drawable/bell_icon"
        android:title="Notification"
        app:showAsAction="always" />
</menu>

MainActivity.java:

private int hot_number = 2;
    private TextView ui_hot = null;

@Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu_action_bar, menu);
        final View menu_hotlist = menu.findItem(R.id.menu_hotlist).getActionView();
        ui_hot = (TextView) menu_hotlist.findViewById(R.id.hotlist_hot);
        updateHotCount(hot_number);
        new MyMenuItemStuffListener(menu_hotlist, "Show hot message") {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Hello",Toast.LENGTH_SHORT).show();
            }
        };
        return super.onCreateOptionsMenu(menu);
    }

    public void updateHotCount(final int new_hot_number) {
        hot_number = new_hot_number;
        if (ui_hot == null) return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (new_hot_number == 0)
                    ui_hot.setVisibility(View.INVISIBLE);
                else {
                    ui_hot.setVisibility(View.VISIBLE);
                    ui_hot.setText(Integer.toString(new_hot_number));
                }
            }
        });
    }

    static abstract class MyMenuItemStuffListener implements View.OnClickListener, View.OnLongClickListener {
        private String hint;
        private View view;

        MyMenuItemStuffListener(View view, String hint) {
            this.view = view;
            this.hint = hint;
            view.setOnClickListener(this);
            view.setOnLongClickListener(this);
        }

        @Override abstract public void onClick(View v);

        @Override public boolean onLongClick(View v) {
            final int[] screenPos = new int[2];
            final Rect displayFrame = new Rect();
            view.getLocationOnScreen(screenPos);
            view.getWindowVisibleDisplayFrame(displayFrame);
            final Context context = view.getContext();
            final int width = view.getWidth();
            final int height = view.getHeight();
            final int midy = screenPos[1] + height / 2;
            final int screenWidth = context.getResources().getDisplayMetrics().widthPixels;
            Toast cheatSheet = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
            if (midy < displayFrame.height()) {
                cheatSheet.setGravity(Gravity.TOP | Gravity.RIGHT,
                        screenWidth - screenPos[0] - width / 2, height);
            } else {
                cheatSheet.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
            }
            cheatSheet.show();
            return true;
        }
    }

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        androidx.appcompat.widget.Toolbar toolbar = findViewById(R.id.notification_layout);
        setSupportActionBar(toolbar);
}
private int hot_number=2;
私有文本视图ui_hot=null;
@凌驾
公共布尔onCreateOptions菜单(最终菜单){
MenuInflater MenuInflater=getMenuInflater();
菜单充气(右菜单菜单动作栏菜单);
final View menu_hotlist=menu.findItem(R.id.menu_hotlist).getActionView();
ui_hot=(TextView)菜单_hotlist.findViewById(R.id.hotlist_hot);
updateHotCount(热处理编号);
新建MyMenuItemStuffListener(菜单\热列表,“显示热消息”){
@凌驾
公共void onClick(视图v){
Toast.makeText(MainActivity.this,“Hello”,Toast.LENGTH_SHORT.show();
}
};
返回super.onCreateOptions菜单(菜单);
}
public void updateHotCount(最终整数新\热\号){
热号=新热号;
if(ui_hot==null)返回;
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
if(新的\u热\u编号==0)
ui_hot.setVisibility(视图不可见);
否则{
ui_hot.setVisibility(View.VISIBLE);
ui_hot.setText(Integer.toString(new_hot_number));
}
}
});
}
静态抽象类MyMenuItemStuffListener实现View.OnClickListener、View.OnLongClickListener{
私有字符串提示;
私人视野;
MyMenuItemStuffListener(视图、字符串提示){
this.view=视图;
this.hint=提示;
view.setOnClickListener(这个);
view.setOnLongClickListener(此);
}
@重写抽象公共void onClick(视图v);
@仅长按一下即可替代公共布尔值(视图v){
最终整数[]屏幕位置=新整数[2];
final Rect displayFrame=new Rect();
view.getLocationOnScreen(屏幕位置);
view.getWindowVisibleDisplayFrame(displayFrame);
final Context=view.getContext();
final int width=view.getWidth();
final int height=view.getHeight();
最终int midy=屏幕位置[1]+高度/2;
final int screenWidth=context.getResources().getDisplayMetrics().widthPixels;
Toast-cheatSheet=Toast.makeText(上下文、提示、Toast.LENGTH\u-SHORT);
if(midy
有人能帮我吗

编辑:

操作\u栏\u通知\u icon.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center"
    android:layout_gravity="center"
    android:clickable="true"
    style="@android:style/Widget.ActionButton">

    <ImageView
        android:id="@+id/hotlist_bell"
        android:src="@drawable/bell_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_margin="0dp"
        android:contentDescription="bell"
        />

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/hotlist_hot"
        android:layout_width="wrap_content"
        android:minWidth="17sp"
        android:textSize="12sp"
        android:textColor="#ffffffff"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@null"
        android:layout_alignTop="@id/hotlist_bell"
        android:layout_alignRight="@id/hotlist_bell"
        android:layout_marginRight="0dp"
        android:layout_marginTop="3dp"
        android:paddingBottom="1dp"
        android:paddingRight="4dp"
        android:paddingLeft="4dp"
        android:background="@drawable/rounded_square"/>
</RelativeLayout>


您能否显示此-@layout/action\u bar\u通知的代码_icon@AlexRmcf我已经在编辑部分粘贴了代码。你能显示这个-@layout/action\u bar\u notification的代码吗_icon@AlexRmcf我已将代码粘贴到编辑部分