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:41

You should find that the question I have posted here is the likey problem and I have also posted the solution.

Why my Android ActionBar doesn't update when it is explictily changed

查看更多
Root(大扎)
3楼-- · 2020-06-09 07:42

I guess this problem is solved by refreshing action bar UI.

So I had solved with below codes.

ActionBar actionBar = getActivity().getActionBar();
actionBar.setTitle(title);

// for refreshing UI
actionBar.setDisplayHomeAsUpEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
查看更多
狗以群分
4楼-- · 2020-06-09 07:42

A question from 2013, still being a thing.

Well, for those still shocked to occasionally see this happening even though you're happily using Androidx, all recent artifacts, etc. I found something that fixed it and it doesn't require custom titles, or anything special.

Bear in mind that this may not be your case but it appears to have magically fixed this issue in an instant.

In my case, the Toolbar, was set as supportActionBar as usual, and it was contained in a CoordinatorLayout + AppBar. At first I was going to blame those, because why not, but before doing so I decided to inspect the Toolbar's source code and debug what was happening.

I couldn't tell (but I did reach the code that was causing this ellipsis):

/**
 * Set the title of this toolbar.
 *
 * <p>A title should be used as the anchor for a section of content. It should
 * describe or name the content being viewed.</p>
 *
 * @param title Title to set
 */
public void setTitle(CharSequence title) {
    if (!TextUtils.isEmpty(title)) {
        if (mTitleTextView == null) {
            final Context context = getContext();
            mTitleTextView = new AppCompatTextView(context);
            mTitleTextView.setSingleLine();
            mTitleTextView.setEllipsize(TextUtils.TruncateAt.END);
            if (mTitleTextAppearance != 0) {
                mTitleTextView.setTextAppearance(context, mTitleTextAppearance);
            }
            if (mTitleTextColor != null) {
                mTitleTextView.setTextColor(mTitleTextColor);
            }
        }
        if (!isChildOrHidden(mTitleTextView)) {
            addSystemView(mTitleTextView, true);
        }
    } else if (mTitleTextView != null && isChildOrHidden(mTitleTextView)) {
        removeView(mTitleTextView);
        mHiddenViews.remove(mTitleTextView);
    }
    if (mTitleTextView != null) {
        mTitleTextView.setText(title);
    }
    mTitleText = title;
}

This is straight from the latest Toolbar.java in appCompat 1.1.0 (latest at this time in October 2019).

Nothing special, but you can see that when the TitleTextView is first created, it's set to singleLine and to use an Ellipsize at the END... (or right in LTR languages).

However, I noticed that when I was calling setTitle, the code didn't go thought that, presumably because the titleTextView was created before (and at that time, set to singleLine with Ellipsis); when I called setTitle, all the code was doing was mTitleTextView.setText(title). No amount of invalidate(), forceLayout(), requestLayout() to the toolbar would fix this.

Upon further inspection, I noticed that my layout (the Coordinator layout), was contained in a RelativeLayout.

The view Hierarchy looked like:

<RelativeLayout>
   <CoordinatorLayout>
       <AppBarLayout>
          <androidx.appcompat.widget.Toolbar />
       </AppBarLayout>
       <androidx.viewpager2.widget.ViewPager2 />
       <com.google.android.material.bottomnavigation.BottomNavigationView />
   </CoordinatorLayout>
</RelativeLayout>

I never liked RelativeLayout, I thought it was a horrible design to begin with, but also faced various issues with it since I can remember, and that it was very bad at handling its children.

For fun, I decided: "what if I change this RelativeLayout into a ConstraintLayout?"

I "right clicked" in the RelativeLayout in the Android Studio visual editor -> convert to ConstraintLayout, next, finish (Didn't touch the defaults).

Now it looks like this:

<ConstraintLayout>
   <CoordinatorLayout>
       <AppBarLayout>
          <androidx.appcompat.widget.Toolbar />
       </AppBarLayout>
       <androidx.viewpager2.widget.ViewPager2 />
       <com.google.android.material.bottomnavigation.BottomNavigationView />
   </CoordinatorLayout>
</ConstraintLayout>

Same, but the ROOT is no longer a relative layout.

I made two manual changes to the generated XML:

This is how the CoordinatorLayout looked after the conversion:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent">

It looked right in the preview, but it caught my attention because one _should not use match_parent in a child of ConstraintLayout... so... I changed it to:

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

Which is exactly the same I had before (the coordinator layout taking up the whole screen).

This is thanks to the new ConstraintLayout having these:

    android:layout_width="match_parent"
    android:layout_height="match_parent"

And this magically solved my truncated titles once and for all.

tl;dr

Do not use RelativeLayouts anymore in 2019-onward, they are bad from a lot of points of view. If your toolbar is contained in a relative layout, consider migrating to a ConstraintLayout or any other ViewGroup.

查看更多
Ridiculous、
5楼-- · 2020-06-09 07:46

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

查看更多
放我归山
6楼-- · 2020-06-09 07:50

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.

查看更多
霸刀☆藐视天下
7楼-- · 2020-06-09 07:59

Because ListView / GridView / RecyclerView is called notifyDataSetChanged() at the same time. For some reason, this would cause layout update, which I guess result in the chaos of the action bar title's layout update.

Basically, you can postDelay() to update title to fix this problem, but I don't like this way.

I decide to override the BaseAdapter.notifyDateSetChanged() and update item view by hand. It works:

public abstract class BaseListAdapter<BEAN extends Unique, VH extends BaseListViewHolder<BEAN>> extends BaseAdapter {
    // data
    private List<BEAN> mList;
    // view holder
    private List<VH> mViewHolderList = new ArrayList<>();

    protected abstract void onBindViewHolder(VH holder, int position);

    protected abstract VH onCreateViewHolder(ViewGroup parent, int viewType);

    ...

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH viewHolder;
        if (convertView == null) {
            viewHolder = onCreateViewHolder(parent, getItemViewType(position));
            convertView = viewHolder.itemView;
            convertView.setTag(viewHolder);
            mViewHolderList.add(viewHolder);
        } else {
            //noinspection unchecked
            viewHolder = (VH) convertView.getTag();
        }

        viewHolder.setData(getItem(position));
        viewHolder.setPosition(position);
        onBindViewHolder(viewHolder, position);

        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        if (mViewHolderList.size() == 0) {
            super.notifyDataSetChanged();
        } else {
            for (VH vh : mViewHolderList) {
                onBindViewHolder(vh, vh.getPosition());
            }
        }
    }

}

public class BaseListViewHolder<T> {
    public View itemView;
    private T data;

    private int position;

    public BaseListViewHolder(View itemView) {
        this.itemView = itemView;
    }

    @Override
    public T getData() {
        return data;
    }

    @Override
    public void setData(T data) {
        this.data = data;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }
}

Sorry for my bad english.

查看更多
登录 后发表回答