android - 如何创建半透明的圆形工具栏/操作栏项目?

标签 android android-actionbar material-design android-collapsingtoolbarlayout

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

enter image description here

你会把它添加到项目图标本身吗(首选 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/

相关文章:

android - 编译语言res文件时出现AAPT错误

android - 测量 TextInputLayout 错误容器的高度

android - 更改 AppCompat SearchView 建议背景

android - FloatingActionButton 展开成一个新的 Activity

清除错误后,Android TextInputLayout 保持红色

android - 如何制作可变高度的 CardViews 的网格布局?

java - 如何向前移动/迭代到数组中的下一个值(java/android)

android - 选取框文本在小部件中不起作用

android - 如何自定义选项卡操作栏

android - 操作栏中的自定义主页图标 Sherlock