【问题标题】:Android Multiline Toolbar TitleAndroid 多行工具栏标题
【发布时间】:2021-12-14 00:43:21
【问题描述】:

我有一个工具栏,在横向模式下它不像往常那样宽,但它的高度比平时高,因此我想将标题设置为多行(准确地说是 2 行)在景观中。我尝试了一些将 TextView 放在工具栏内的方法,但是当我尝试访问 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" />

【问题讨论】:

    标签: java android android-toolbar


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我真的没有明确的字幕,只是一个很长的标题。设置字幕需要使用 2 个不同的字符串(1 个用于标题,1 个用于副标题),但我只有一个字符串,所以我看不出这有什么帮助。
    • 如果我要使用更新后的答案,我会以相同的方式设置标题还是必须以不同的方式来设置?
    • @DennisvanOpstal 在您的工具栏中删除此行 android:title="@string/menuLabel1a" 并将其添加到 TextView。它解决了你的问题。
    • 这只是一个临时标题,我以编程方式在我的 Activity 类的 onResume 中设置了标题,例如getSupportActionBar().setTitle("This is a pretty long piece of text, I hope it fits");
    • @DennisvanOpstal 我已经发布了其他解决方案也可以尝试一下。
    【解决方案2】:

    找到适合我的解决方案:

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

    根据Toolbar 实现,标题TextView 是在设置文本时创建的。 在标题设置后调用此扩展很重要。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多