如何使工具栏/操作栏中的项目具有半透明圆形背景,如最新的 Google map 应用(屏幕截图)所示
你会把它添加到项目图标本身吗(首选 XML-Drawable)?您会使用普通的 menu-xml 来创建它吗?我想将它与 CollapsingToolbarLayout 一起使用,并在工具栏折叠时隐藏圆形背景。
感谢您提供有关如何制作此内容的任何想法和提示:)
最佳答案
将 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
来监听应用栏展开/折叠的状态,您可以更改工具栏图标项
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;
}
}
});
关于android - 如何创建半透明的圆形工具栏/操作栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54008678/