Android多行工具栏标题

丹尼斯·范·欧普斯塔(Dennis van Opstal)

我有一个工具栏,该工具栏在横向模式下不像通常那样宽,但它的高度比平常大,因此,我想在横向模式下将标题设置为多行(准确地说是2行)。我已经尝试了一些将TextView放入工具栏的方法,但是当我尝试访问TextView设置其标题时(由于标题是可变的),在使用findViewById之后我得到了一个N​​ullPointer。

这是当前情况的一些代码(不带多行):

@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" />
哈莎德·潘苏里亚

试试这个 :

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android中工具栏标题中的多行标题

来自分类Dev

Android中工具栏标题中的多行标题

来自分类Dev

在android中更改工具栏标题

来自分类Dev

android标题受其他标题影响(工具栏)

来自分类Dev

温泉-多行工具栏

来自分类Dev

设置工具栏标题

来自分类Dev

使用Espresso测试工具栏标题文本-Android

来自分类Dev

标题等未显示在Android工具栏中

来自分类Dev

Android:何时设置工具栏标题表单片段?

来自分类Dev

Android工具栏:横向模式下的小标题文本

来自分类Dev

从Android中的片段在工具栏中设置标题

来自分类Dev

Android Lollipop,从工具栏中的标题添加弹出菜单

来自分类Dev

android标题不会显示在工具栏中

来自分类Dev

Android-将工具栏的标题居中

来自分类Dev

Android:如何使文本与标题文本工具栏左对齐?

来自分类Dev

Android:何时设置工具栏标题形式的Fragment?

来自分类Dev

Android-工具栏标题的重力不正确

来自分类Dev

Notepad ++多行文件工具栏

来自分类Dev

如何与工具栏的标题对齐?

来自分类Dev

工具栏标题不居中

来自分类Dev

更改工具栏中的标题颜色?

来自分类Dev

折叠工具栏标题消失

来自分类Dev

片段中的工具栏标题

来自分类Dev

工具栏标题显示在底部

来自分类Dev

Android ActionBar或工具栏

来自分类Dev

Android工具栏样式

来自分类Dev

Android工具栏更改

来自分类Dev

Android查找工具栏

来自分类Dev

在标题栏工具栏android中添加菜单项文本和文本