3

请看: http: //i.imgur.com/iwikd69.png(单“lorem”是副标题)

我正在使用 v7.21 appcompat 库。我想为工具栏设置一个标题,如果它太长,那么它应该像现在一样显示(附图中的上例),只有 1 行,如果不合适,它会以 ... 结尾。但是我希望工具栏展开(然后分别折叠)onClick(可能以某种方式动画)并显示完整标题(下例)

现在我在这里错过了一些事情:

  • 我没有看到任何方法可以告诉我标题是否合适。(getSupportActionBar().isTitleTruncated() 即使标题不合适也会返回 false,但也许这不是我需要的方法,它甚至不是Toolbar类的方法)

  • 我似乎无法以编程方式设置工具栏的高度(即使我可以,动画它会更痛苦,因为我的目标是> = api15)

是否可以执行我想要的,或者我应该找到不同的解决方案?

谢谢

4

1 回答 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" />

于 2015-09-29T06:46:03.770 回答