繁体   English   中英

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

[英]How to create semi-transparent circular toolbar / actionbar items?

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

在此处输入图片说明

您是否会将其添加到项目图标本身(首选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;
        }
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM