gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:01:11 25 4
gpt4 key购买 nike

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

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

现在我想念这里的一些东西:

  • 我没有看到任何方法可以告诉我标题是否合适。 (即使标题不合适,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" />

关于java - 是否可以为工具栏设置多行标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26549139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com