Action Bar Title with more than one line?

前端 未结 4 1934
清歌不尽
清歌不尽 2020-12-17 15:59

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

相关标签:
4条回答
  • 2020-12-17 16:24

    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());
    
    }
    
    0 讨论(0)
  • 2020-12-17 16:34

    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.

    0 讨论(0)
  • 2020-12-17 16:37

    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.");
    
    0 讨论(0)
  • 2020-12-17 16:40

    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);
    }
    
    0 讨论(0)
提交回复
热议问题