Answers:
您可以添加OnOffsetChangedListener
,以AppBarLayout
确定何时CollapsingToolbarLayout
折叠或展开并设置其标题。
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isShow = true;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
collapsingToolbarLayout.setTitle("Title");
isShow = true;
} else if(isShow) {
collapsingToolbarLayout.setTitle(" ");//careful there should a space between double quote otherwise it wont work
isShow = false;
}
}
});
var isShow = true
var scrollRange = -1
appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { barLayout, verticalOffset ->
if (scrollRange == -1){
scrollRange = barLayout?.totalScrollRange!!
}
if (scrollRange + verticalOffset == 0){
collapsingToolbarLayout.title = "Title Collapse"
isShow = true
} else if (isShow){
collapsingToolbarLayout.title = " " //careful there should a space between double quote otherwise it wont work
isShow = false
}
})
我尝试了dlohani的解决方案,但由于褪色而不喜欢它。使用此解决方案,您可以完全消除褪色。
诀窍是创建一个新样式,使其textSize等于0.1sp或0sp(这在SDK <19时崩溃)并且textColor透明:
对于SDK <19
<style name="CollapsingToolbarLayoutExpandedTextStyle" parent="AppTheme">
<item name="android:textColor">@android:color/transparent</item>
<item name="android:textSize">0.1sp</item>
</style>
对于SDK> = 19
<style name="CollapsingToolbarLayoutExpandedTextStyle" parent="AppTheme">
<item name="android:textColor">@android:color/transparent</item>
<item name="android:textSize">0sp</item>
</style>
然后将其应用于布局中的CollapsingToolbarLayout:
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleTextAppearance="@style/CollapsingToolbarLayoutExpandedTextStyle"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
通过将以下属性添加到xml布局中,我能够获得所需的效果:
app:expandedTitleTextAppearance="@android:color/transparent"
所以我的CollapsingToolbarLayout看起来像这样
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
我有一个更简单的答案:
final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
collapsingToolbarLayout.setTitle("Your Title");
collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.transperent)); // transperent color = #00000000
collapsingToolbarLayout.setCollapsedTitleTextColor(Color.rgb(0, 0, 0)); //Color of your title
编码愉快!
mCollapsingToolbarLayout.setExpandedTitleColor(Color.argb(255,0,0,0))
该代码对我有用:使用color.parse color,因为如果您的背景色不同,则替换为白色,并且标题不显示
collapsingToolbarLayout.setExpandedTitleColor(Color.parseColor("#00FFFFFF"));
或者您可以使用透明
collapsingToolbarLayout.setExpandedTitleColor(Color.TRANSPARENT);
我成功添加了一个褪色的textview,只需在工具栏中添加一个text view,然后根据appbar回调中的verticalOffset将其设置为alpha
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
mTitleTextView.setAlpha(Math.abs(verticalOffset / (float)
appBarLayout.getTotalScrollRange()));
}
});
这也是使用API 23的最简单且可行的解决方案:
app:expandedTitleTextAppearance必须继承TextAppearance。
因此,在您的styles.xml中添加以下行:
<style name="TransparentText" parent="@android:style/TextAppearance">
<item name="android:textColor">#00000000</item>
</style>
然后,在您的CollapsingToolbarLayout中,添加此行。
app:expandedTitleTextAppearance="@style/TransparentText"
那就是所有人!
下面的解决方案完美地工作。
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (Math.abs(verticalOffset)-appBarLayout.getTotalScrollRange() == 0)
{
// Collapsed
setTitle("Title To Show");
}
else
{
// Expanded
setTitle("");
}
}
});
这是我的解决方案:
collapsingToolbar.setCollapsedTitleTextAppearance(R.style.personal_collapsed_title);
collapsingToolbar.setExpandedTitleTextAppearance(R.style.personal_expanded_title);
<style name="personal_collapsed_title">
<item name="android:textSize">18sp</item>
<item name="android:textColor">@color/black</item>
</style>
<style name="personal_expanded_title">
<item name="android:textSize">0sp</item>
</style>
在我看来,这样的解决方案会更优雅。
public class MyCollapsingToolbarLayout extends CollapsingToolbarLayout {
private final int toolbarId;
@Nullable private Toolbar toolbar;
public MyCollapsingToolbarLayout(Context context, AttributeSet attrs) {
super(context, attrs);
setTitleEnabled(false);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CollapsingToolbarLayout, 0,
R.style.Widget_Design_CollapsingToolbar);
toolbarId = a.getResourceId(android.support.design.R.styleable.CollapsingToolbarLayout_toolbarId, -1);
a.recycle();
}
@Override public void setScrimsShown(boolean shown, boolean animate) {
super.setScrimsShown(shown, animate);
findToolbar();
if (toolbar != null) {
toolbar.setTitleTextColor(shown ? Color.WHITE : Color.TRANSPARENT);
}
}
private void findToolbar() {
if (toolbar == null) {
toolbar = (Toolbar) findViewById(toolbarId);
}
}
}
用法看起来像这样
<butter.droid.widget.BurtterCollapsingToolbarLayout
app:toolbarId="@+id/toolbar"
...>
也可以淡出/淡入文本,而不仅仅是显示或隐藏文本。
这对我有用。
final Toolbar tool = (Toolbar)findViewById(R.id.toolbar);
CollapsingToolbarLayout c = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.app_bar_layout);
tool.setTitle("");
setSupportActionBar(tool);
c.setTitleEnabled(false);
appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
boolean isVisible = true;
int scrollRange = -1;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (scrollRange == -1) {
scrollRange = appBarLayout.getTotalScrollRange();
}
if (scrollRange + verticalOffset == 0) {
tool.setTitle("Title");
isVisible = true;
} else if(isVisible) {
tool.setTitle("");
isVisible = false;
}
}
});
这是适合我的kotlin版本:
appbar.addOnOffsetChangedListener(object : OnOffsetChangedListener {
var isShow = true
var scrollRange = -1
override fun onOffsetChanged(appBarLayout: AppBarLayout, verticalOffset: Int) {
if (scrollRange == -1) scrollRange = appBarLayout.totalScrollRange
if (scrollRange + verticalOffset == 0) {
toolbarLayout.title = "Title"
isShow = true
} else if (isShow) {
toolbarLayout.title = " " //These quote " " with _ space is intended
isShow = false
}
}
})
只需添加以下代码:
CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collaps_main);
collapsingToolbarLayout.setExpandedTitleColor(ContextCompat.getColor(this , android.R.color.transparent));