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 07:36

I dont think there is much you can do about this. Seems to be the action bar title has a fairly limited width and anything over that gets truncated.

I guess one way round it would be to display: "My reqs (20)" rather than "My requests (20)" but I appreciate that is not ideal.

查看更多
Explosion°爆炸
3楼-- · 2020-06-09 07:37

I solved this problem using a custom title as described in this post.

This is the code I use to change the title when a tab changes

((TextView) actionBar.getCustomView().findViewById(R.id.title)).setText(someTitle); 

Note that this solution places the title to the right of the tabs in landscape mode when using actionbar tabs.

查看更多
smile是对你的礼貌
4楼-- · 2020-06-09 07:37

I know this question was posted a long time ago, but I recently ran into this issue and it caused a hell of a headache. I'm working on a Galaxy Note 10.1 and my title was "BidItems" and I only had the action overflow item visible, yet sometimes, "BidItems" became "BidIt..." which is just ridiculous. After much testing, I found that the reason for this truncating behavior in my application is that I was calling a method using ((MyActivity) getApplication()).setTitle(); method from one of my fragments. The setTitle() method in my Activity calls getActionBar().setTitle(). As soon as I called this method, my title was truncated for no reason. But simply calling setTitle() from my activity worked just fine.

I really hope this saves people the headache it caused me.

查看更多
唯我独甜
5楼-- · 2020-06-09 07:37

In my case the text was being truncated but not with ellipsis (...), the last character was always cut, like this:

screenshot

I found out that changing the toolbar width from "wrap_content", to "match_parent" solves the issue:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_weight="1"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
查看更多
走好不送
6楼-- · 2020-06-09 07:37

I will add that you can totally use the action bar title on 2 lines to get more space:

//Set the title over 2 lines if needed:
    int titleId = Resources.getSystem()
            .getIdentifier("action_bar_title", "id", "android");
    if (titleId > 0) {
        TextView title = (TextView) findViewById(titleId);
        title.setSingleLine(false);
        title.setMaxLines(2);
        //          title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
    }

I am using it on my application and I am happy with it :)

Credit to this post

查看更多
我想做一个坏孩纸
7楼-- · 2020-06-09 07:39

Your AndroidManifest.xml has your activity definition like so

<activity
        android:name="com.example.DetailsView"
        android:label="Details">
</activity>

If you change android:label="Whatever" to android:label=" ",

It'll work. I assume this happens because android creates a TextView to display label and the width is not re-sized when setTitle is called. So by setting the initial title to have more characters than you're going to set the title with, it will not be truncated.

查看更多
登录 后发表回答