java 更改可折叠工具栏标题的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31302121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to change Collapseable Toolbar title
提问by Rockwell
When I change title of collapsed collapsingToolbar, the title doesn't change.
当我更改折叠的 collapsingToolbar 的标题时,标题不会改变。
I've tried getSupportActionBar.setTitle and collapseToolbar.setTitle, but it didn't help. Tell me, what's the problem ?
我试过 getSupportActionBar.setTitle 和 collapseToolbar.setTitle,但没有帮助。告诉我,有什么问题?
回答by doubleA
I believe that thisissue describes what you are experiencing. I also had this issue and solved it today. Essentially the code that handles the collapsing text only update the text when the current text is null or the size of the text changes. Currently this is a bug that is closed and the patch is scheduled for a future release of the design library. For now use my workaround of just changing the size of the text and then changing it back.
我相信这个问题描述了您所遇到的情况。我也遇到了这个问题,今天解决了。本质上,处理折叠文本的代码仅在当前文本为空或文本大小更改时更新文本。目前这是一个已关闭的错误,该补丁计划在设计库的未来版本中发布。现在使用我的解决方法,只需更改文本的大小,然后再将其更改回来。
This is what I have
这就是我所拥有的
private void setCollapsingToolbarLayoutTitle(String title) {
mCollapsingToolbarLayout.setTitle(title);
mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBar);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBar);
mCollapsingToolbarLayout.setExpandedTitleTextAppearance(R.style.ExpandedAppBarPlus1);
mCollapsingToolbarLayout.setCollapsedTitleTextAppearance(R.style.CollapsedAppBarPlus1);
}
in styles.xml I have
在styles.xml我有
<style name="ExpandedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBar" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>
<style name="ExpandedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">28.5sp</item>
<item name="android:textColor">#000</item>
<item name="android:textStyle">bold</item>
</style>
<style name="CollapsedAppBarPlus1" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">24.5sp</item>
<item name="android:textColor">@color/white</item>
<item name="android:textStyle">normal</item>
</style>
Happy Coding.
快乐编码。
EDIT: The below code is from the collapsingtext helper that is used inside the collapsing toolbar layout to control the text of that view.
编辑:以下代码来自折叠工具栏布局内用于控制该视图文本的折叠文本助手。
if(availableWidth > 0.0F) {
updateDrawText = this.mCurrentTextSize != newTextSize;
this.mCurrentTextSize = newTextSize;
}
if(this.mTextToDraw == null || updateDrawText) {
this.mTextPaint.setTextSize(this.mCurrentTextSize);
CharSequence title = TextUtils.ellipsize(this.mText, this.mTextPaint, availableWidth, TruncateAt.END);
if(this.mTextToDraw == null || !this.mTextToDraw.equals(title)) {
this.mTextToDraw = title;
}
this.mTextWidth = this.mTextPaint.measureText(this.mTextToDraw, 0, this.mTextToDraw.length());
}
the offending lines are updateDrawText = this.mCurrentTextSize != newTextSize;
which sets the boolean to determine if we change the text in this line if(this.mTextToDraw == null || updateDrawText) {
So when the collapsing toolbar layout recalculates its view the determining factor to set the text is the textsize. If you did not change the text size then your collapsing toolbar layout title will not change untill it is collapsed or expanded from collapsed position
违规行是updateDrawText = this.mCurrentTextSize != newTextSize;
设置布尔值以确定我们是否更改此行中的文本if(this.mTextToDraw == null || updateDrawText) {
因此当折叠工具栏布局重新计算其视图时,设置文本的决定因素是文本大小。如果您没有更改文本大小,那么您的折叠工具栏布局标题将不会更改,直到它折叠或从折叠位置展开
回答by Campino
Define you style, such as:
定义您的风格,例如:
<style name="CollapsedAppBarTopic" parent="@android:style/TextAppearance.Medium">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">normal</item>
</style>
Then in you xml:
然后在你的xml中:
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/cardview_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="false"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="48dp"
app:expandedTitleMarginStart="16dp"
app:expandedTitleMarginBottom="16dp"
app:expandedTitleTextAppearance="@style/CollapsedAppBarTopic"
>