【问题标题】:How to create semi-transparent circular toolbar / actionbar items?如何创建半透明的圆形工具栏/操作栏项目?
【发布时间】:2019-05-29 05:44:26
【问题描述】:

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

您会将它添加到项目图标本身(首选 XML-Drawable)吗?你会使用普通的 menu-xml 来创建它吗?我想将它与 CollapsingToolbarLayout 一起使用,并在工具栏折叠时隐藏圆形背景。

感谢任何关于如何制作这个的想法和提示:)

【问题讨论】:

  • 你找到解决办法了吗?
  • 不,我放弃了。不值得为我正在做的项目付出努力

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


【解决方案1】:

将 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;
        }
    }
});

【讨论】:

  • 但是默认的后退按钮和溢出菜单呢?
  • 好吧,我知道如何使用 onOffsetChanged。这是否意味着您将实现自定义溢出菜单和后退按钮?
  • 你不需要创建自定义,你可以改变这个项目的默认图标
  • 如何更改默认的溢出菜单和后退按钮?有没有一种不太hacky的方法?
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多