Android 多行工具栏标题

10
我有一个工具栏,当处于横屏模式时,它的宽度不如通常那么宽,但是它比通常更高,因此我想在横屏模式下将标题设置为多行(确切地说是2行)。我尝试了一些方法,将TextView放在Toolbar内部,但是当我尝试访问TextView以设置其标题(因为标题是可变的)时,使用findViewById后会出现NullPointer。
以下是当前情况的一些代码(没有多行):
@Override
protected void onResume() {
    super.onResume();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar2);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = (int)( size.x * 0.37);
        int height =  Math.round(size.x * 0.63f * 0.25f);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
        toolbar.setLayoutParams(params);
    }
    if (executeOnResume) {
        currentProject = getIntent().getParcelableExtra("Project");
        getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
        // A lot of irrelevant code...
        //...
        //...
    } else { loadStuff();}
}

这里是工具栏的xml代码:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar2"
        android:title="@string/menuLabel1a"
        app:layout_widthPercent="37%"
        app:layout_heightPercent="26%"
        android:gravity="top"
        app:titleTextAppearance="@style/toolbarTitleText"
        android:background="@color/greyLight"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/AppTheme.PopupOverlay" />
2个回答

16

试试这个:

 <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar2"
    android:layout_width="match_parent"
    android:layout_height="@dimen/double_height_toolbar"
    android:gravity="top"
    app:titleTextAppearance="@style/toolbarTitleText"
    android:background="@color/greyLight"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/product_details_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:title="@string/menuLabel1a"
            android:paddingTop="@dimen/padding_normal"
            style="@android:style/TextAppearance.Holo.Widget.ActionBar.Title.Inverse"
            android:maxLines="2" />

</android.support.v7.widget.Toolbar>

其他解决方案是将其应用于ActionBar

ActionBar的默认TextView不支持自动换行,并且没有公开的方法可以设置它进行换行。因此,您可以创建一个灵活的布局,就像这样: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>

然后将此设置为您的 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.");

我没有一个明确的副标题,只有一个非常长的标题。设置副标题需要使用两个不同的字符串(一个用于标题,一个用于副标题),但我只有一个字符串,所以我不认为这真的有帮助。 - Dennis van Opstal
如果我使用更新后的答案,我需要以相同的方式设置标题还是需要以不同的方式进行? - Dennis van Opstal
@DennisvanOpstal 在你的工具栏中删除这行代码 android:title="@string/menuLabel1a" 并将其添加到 TextView 中。它会解决你的问题。 - Harshad Pansuriya
这只是一个临时标题,我会在我的Activity类的onResume方法中以编程方式设置标题,如下所示:getSupportActionBar().setTitle("这是一段相当长的文本,希望它能够适应"); - Dennis van Opstal
@DennisvanOpstal 我已经发布了另一种解决方案,请尝试。 - Harshad Pansuriya
@DennisvanOpstal 很高兴能帮助你。 - Harshad Pansuriya

0
发现了一个对我有效的解决方案:

fun Toolbar.disableTitleSingleLine() {
    for (i in 0..childCount) {
        val child = getChildAt(i)
        if (child is TextView) {
            child.isSingleLine = false
        }
    }
}

根据 Toolbar 的实现,当设置文本时,标题 TextView 会被创建。 在设置标题后调用此扩展非常重要。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接