折叠工具栏展开和折叠状态下的后退按钮颜色不同
different back button color in expanded and collapsed states of Collapsing Tollbar
如何在 Collapsing Tollbar 的展开和折叠状态下设置不同的后退按钮颜色?
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/Theme.TopMovie.AppBarOverlay"
app:contentScrim="?attr/colorOnPrimary"
app:expandedTitleGravity="bottom|center_horizontal"
app:expandedTitleMarginEnd="16dp"
app:scrimAnimationDuration="100"
app:expandedTitleMarginStart="16dp"
app:expandedTitleTextAppearance="@style/TitleTextStyle"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:maxLines="3"
app:titleCollapseMode="scale">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
tools:title="TitleTitle"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<style name="Theme.TopMovie.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="expandedTitleTextColor">@color/white</item>
<item name="collapsedTitleTextColor">?attr/colorOnSecondary</item>
<item name="colorControlNormal">?attr/colorOnSecondary</item>
</style>
colorControlNormal 在所有状态下设置颜色,但我需要不同的颜色。
您可以使用 onOffsetChanged 检测 AppBarLayout 折叠或展开。
下面是 onOffsetChanged 监听器的示例代码 Class :
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
然后就可以使用了:
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
Log.d("STATE", state.name());
}
});
如何在 Collapsing Tollbar 的展开和折叠状态下设置不同的后退按钮颜色?
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/main.collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:theme="@style/Theme.TopMovie.AppBarOverlay"
app:contentScrim="?attr/colorOnPrimary"
app:expandedTitleGravity="bottom|center_horizontal"
app:expandedTitleMarginEnd="16dp"
app:scrimAnimationDuration="100"
app:expandedTitleMarginStart="16dp"
app:expandedTitleTextAppearance="@style/TitleTextStyle"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:maxLines="3"
app:titleCollapseMode="scale">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
tools:title="TitleTitle"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<style name="Theme.TopMovie.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" >
<item name="expandedTitleTextColor">@color/white</item>
<item name="collapsedTitleTextColor">?attr/colorOnSecondary</item>
<item name="colorControlNormal">?attr/colorOnSecondary</item>
</style>
colorControlNormal 在所有状态下设置颜色,但我需要不同的颜色。
您可以使用 onOffsetChanged 检测 AppBarLayout 折叠或展开。 下面是 onOffsetChanged 监听器的示例代码 Class :
public abstract class AppBarStateChangeListener implements AppBarLayout.OnOffsetChangedListener {
public enum State {
EXPANDED,
COLLAPSED,
IDLE
}
private State mCurrentState = State.IDLE;
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
if (i == 0) {
if (mCurrentState != State.EXPANDED) {
onStateChanged(appBarLayout, State.EXPANDED);
}
mCurrentState = State.EXPANDED;
} else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
if (mCurrentState != State.COLLAPSED) {
onStateChanged(appBarLayout, State.COLLAPSED);
}
mCurrentState = State.COLLAPSED;
} else {
if (mCurrentState != State.IDLE) {
onStateChanged(appBarLayout, State.IDLE);
}
mCurrentState = State.IDLE;
}
}
public abstract void onStateChanged(AppBarLayout appBarLayout, State state);
}
然后就可以使用了:
appBarLayout.addOnOffsetChangedListener(new AppBarStateChangeListener() {
@Override
public void onStateChanged(AppBarLayout appBarLayout, State state) {
Log.d("STATE", state.name());
}
});