简体   繁体   中英

Change the size of the text and the position of a single item in the menu

How can I change the size of the text and the position of a single item in the menu?

在此处输入图像描述

I would like the Calculator item to be moved to the left. In addition, I would like to increase its size and make the font bold. How could I do that?

I tried it this way, but I don't know why, the text size does not increase, in addition, I have no idea how I can move this element and bold its font:

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater awesome = getMenuInflater();
    awesome.inflate(R.menu.menu_main, menu);
    for(int i = 0; i < menu.size(); i++) {
        MenuItem item = menu.getItem(i);
        if (item.getItemId() == R.id.blue)
        {
            SpannableString spanString = new SpannableString(menu.getItem(i).getTitle().toString());
            int end = spanString.length();
            spanString.setSpan(new RelativeSizeSpan(1.5f), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            item.setTitle(spanString);
        }
    }
    return true;
}

You are using the menu xml so you are a little more restricted in the styling than if you build it yourself using the adapter pattern. You are trying to add a group title to the menu. In your R.menu.menu_main you can add something like:

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

<item android:title="Communicate">
    <menu>
        <item
            android:id="@+id/nav_conventional"
            android:icon="@drawable/ic_menu_conventional"
            android:title="Conventional" />
        <item
            android:id="@+id/nav_scientific"
            android:icon="@drawable/ic_menu_scientific"
            android:title="Scientific" />
    </menu>
</item>

</menu>

This will give you something like: 在此处输入图像描述

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