Why Android is truncating my ActionBar title?

前端 未结 13 2081
Happy的楠姐
Happy的楠姐 2021-02-13 10:28

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)\".

T

相关标签:
13条回答
  • 2021-02-13 11:04

    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.

    0 讨论(0)
  • 2021-02-13 11:08

    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.

    0 讨论(0)
  • 2021-02-13 11:08

    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.

    0 讨论(0)
  • 2021-02-13 11:09

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

    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" />
    
    0 讨论(0)
  • 2021-02-13 11:11

    If anyone is still interested in a proper answer after more than 2 years I encountered this problem as well recently and realized the reason is that I updated the title from background thread. In Kotlin using supportActionBar?.title = <abc>.
    For some reason the app didn't crash as it usually does with an exception mentioning that you are touching views outside of UI thread. Instead it sets new text, but skips layout pass resulting in wrapping the text.
    So either use Activity.runOnUiThread or View.post or, when using coroutines as I did, use withContext(Dispatchers.Main) to trigger the code that updates the title.

    0 讨论(0)
  • 2021-02-13 11:12

    put setTitle() in onCreateOptionsMenu helps me to solve this problem.

    In your fragment add setHasOptionsMenu(true); in onCreateView(){}

    Then override onCreateOptionsMenu().

    Example:

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        // put getActivity in onCreateOptionsMenu will not return null
        if (getActivity() != null) {
            getActivity().setTitle(getResources().getString(R.string.Studies));
        }
    }
    

    Reference: How to display android actionbar title without truncation occurring

    0 讨论(0)
提交回复
热议问题