请参阅:https://i.stack.imgur.com/KGmVa.png(单曲"lorem"是一个副标题)
我正在使用v7.21 appcompat库。我想为工具栏设置一个标题,如果它太长,那么它应该像现在一样显示(所附图像中的上例),只有1行,并且以…结尾。。。如果不合适。然而,我希望工具栏在单击时展开(然后分别折叠)(可能以某种方式设置了动画)并显示完整的标题(下例)
现在我错过了一些东西:
-
我看不出有什么方法可以告诉我标题是否合适。(getSupportActionBar().isTitleTruncated()即使标题不合适也会返回false,但也许这不是我需要的方法,它甚至不是Toolbar类的方法)
-
我似乎无法用程序设置工具栏的高度(即使我可以,设置动画也会更痛苦,因为我的目标是>=api15)
是否可以执行我想要的内容,或者我应该找到不同的解决方案?
感谢
创建自定义工具栏这是的例子
/**
* 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" />