工具栏是否可以设置多行标题?

3
请查看:http://i.imgur.com/iwikd69.png(单个“lorem”是一个副标题)。
我正在使用v7.21 appcompat库。我想要给工具栏设置一个标题,如果标题太长则应该像现在一样显示(附加的图像中的上例),只显示1行,并以...结束,如果没有合适的位置。但是,我希望在单击时展开工具栏(然后依次折叠),并显示完整的标题(较低的示例)。
现在我错过了一些东西:
  • 我没有看到任何可以告诉我标题是否适合的方法。(即使标题不适合,getSupportActionBar().isTitleTruncated()返回false,但这可能不是我需要的方法,它甚至不是Toolbar类的方法之一)

  • 我似乎无法通过编程方式设置工具栏的高度(即使我可以,动画化将更加痛苦,因为我正在针对>=api15)。

我能否实现我的愿望,还是应该找到其他解决方案?
谢谢
1个回答

0

创建自定义工具栏 这是一个例子

/**
 * Create the Action Bar and set the Custom View to it.
 * Set content insets absolute (0,0) to hide the bottom margin for Action Bar.
 *
 */
public void initializeAppToolbar()
{
    actionbar = getSupportActionBar();
    actionbar.setDisplayShowHomeEnabled(false);
    actionbar.setDisplayShowCustomEnabled(true);
    actionbar.setDisplayShowTitleEnabled(false);

    actionbarView = getLayoutInflater().inflate(R.layout.custom_actionbar,null);
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.MATCH_PARENT);
    actionbar.setCustomView(actionbarView, layoutParams);

    Toolbar parent = (Toolbar) actionbarView.getParent();
    parent.setContentInsetsAbsolute(0, 0);
    parent.setBackgroundResource(R.drawable.top_nav);

    toolbar_title   = (TextView)actionbarView.findViewById(R.id.toolbar_title);

    // set click listener on toolbar_title.. and perform expand and collapse

    // need to check this condition for lollipop and greater version
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().setStatusBarColor(getResources().getColor(R.color.statusBarDark));
    }
}

这是custom_actionbar.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/top_nav"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Profile"
    android:textColor="@android:color/white"
    android:textStyle="normal"
    android:textSize="24sp" />


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