Action Bar Title with more than one line?

2019-01-25 07:04发布

问题:

Is it possible to create the title in the Action Bar with more than one line? I need a double spaced title.

回答1:

I am not quite sure if you were looking for the following, but:

actionBar.setSubtitle(String string) sets a second line with smaller font underneath your main Title.

example:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_left_drawer_item_clicked);
        String value = getIntent().getExtras().getString("drawerItemString");

        ActionBar actionBar = getActionBar();
        // Make sure we're running on Honeycomb or higher to use ActionBar APIs
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        actionBar.setTitle(selectedBook.getTitle());
        actionBar.setSubtitle(selectedBook.getAuthorName());

}


回答2:

I found a better solution that works perfectly up to Android 4.2.2 where the icon and the title are one clickable item.

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);
}

If you are using ActionBarSherlock, use this code:

int titleId = Resources.getSystem()
        .getIdentifier("action_bar_title", "id", "android");
if (titleId == 0) {
    titleId = com.actionbarsherlock.R.id.abs__action_bar_title;
}
TextView title = (TextView) findViewById(titleId);
if (title != null) {
    title.setSingleLine(false);
    title.setMaxLines(2);
    title.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
}


回答3:

The default TextView on the ActionBar does not support line wrapping, and there is no exposed method to set it to wrap. So create a flexible layout, like this: action_bar_title_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<TextView
    android:id="@+id/action_bar_title"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:gravity="center_vertical"
    android:ellipsize="end"
    android:maxLines="2"/>

</LinearLayout>

Then set this as the custom layout on your ActionBar:

ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.action_bar_title_layout);
((TextView) findViewById(R.id.action_bar_title)).setText(
    "This is a long text title that will wrap to multiple lines, when necessary.");


回答4:

No, but you can replace the icon with a separate logo graphic (android:logo and then setDisplayUseLogoEnabled()). You could embed your two-line title as part of the logo graphic and then not show the original title string via setDisplayShowTitleEnabled(). AFAIK, that combination should work.