如何使工具栏/操作栏中的项目具有半透明的圆形背景,如最新的Google Maps应用程序(屏幕截图)所示

您会将其添加到项目图标本身(XML-Drawable优先)吗?您会使用普通的menu-xml来创建它吗?我想将其与CollapsingToolbarLayout一起使用,并在工具栏折叠时隐藏圆形背景。
感谢任何关于如何做到这一点的想法和提示:)
发布于 2019-01-02 23:35:32
将imageView添加到CollapsingToolbarLayout并使用app:layout_collapseMode="parallax"
示例:
<android.support.design.widget.AppBarLayout
android:layout_height="200dp"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways" />
<ImageView
android:src="@drawable/cheese_1"
app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"
android:minHeight="100dp" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>在代码中,使用OnOffsetChangedListener监听appbar展开/折叠的状态,您可以更改工具栏图标项
final AppBarLayout mAppBarLayout = findViewById(R.id.appbar);
mAppBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
private State state;
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
if (verticalOffset == 0) {
if (state != State.EXPANDED) {
//change item rounded icon
}
state = State.EXPANDED;
} else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
if (state != State.COLLAPSED) {
//change item default icon
}
state = State.COLLAPSED;
} else {
if (state != State.IDLE) {
Log.d(TAG,"Idle");
}
state = State.IDLE;
}
}
});https://stackoverflow.com/questions/54008678
复制相似问题