Why Android is truncating my ActionBar title?

2020-06-09 06:57发布

In my app, I change the title in the ActionBar from each fragment displayed. When I first start my apps, I got a list of requests, so my title is "My requests (20)".

Then, when you click on an item in that list, it is replacing a fragment in my Activity and it set the title to "Action".

When I come back to the first view (always in the same Activity), I reset the title to "My requests (20)", but android decide to truncate it. So the result is "My request...".

After much tests, it looks like that Android is shrinking my title when I put a smaller text in it. But it doesn't enlarge it when I put a longer text, even if there is plenty of room.

What can I do to solve this? I would like a better solution than adding spaces at the end of my short titles :)

Here is the code I use to change the title of the ActionBar :

getActivity().getActionBar().setTitle(title);   

13条回答
淡お忘
2楼-- · 2020-06-09 08:00

In my particular case, I'm developing a hybrid app with a complex native menu structure. I'd see this issue intermittently when calling a deeplink from the hybrid content that would update the selected menu option and set the title.

I tried several of the suggested fixes with not luck. But setting a custom view produced a strange result that gave me the feeling that I was dealing with a race condition.

This proved true. I found that simply overriding the activity's setTitle function and wrapping the call to super in a postDelay runnable fixed this for me:

@Override
public void setTitle(final CharSequence title) {
    toolBar.postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.super.setTitle(title);
        }
    }, 200);
}

I'm using toolbar's postDelayed as a convenience method. But you can certainly use a handler here. Hope this helps.

查看更多
登录 后发表回答