java - Android折叠工具栏在折叠时没有隐藏其他元素

标签 java android android-collapsingtoolbarlayout

我在 Android 上有一个布局支持设计折叠工具栏,其中包含 TextView 但是当我折叠我的工具栏时。一些带有工具栏标题的 TextView。 我想隐藏所有其他东西而不是工具栏和标题。 这是我的布局。

<android.support.design.widget.AppBarLayout
    android:id="@+id/MyAppbar"
    android:layout_width="match_parent"
    android:layout_height="256sp"
    android:fitsSystemWindows="true">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:background="#ff009688"
        app:contentScrim="@color/colorPrimary"
        app:expandedTitleMarginTop="100sp"
        app:expandedTitleGravity="center|bottom"
        android:fitsSystemWindows="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            app:srcCompat="@drawable/bgimg"
            app:layout_collapseMode="parallax"/>
        <android.support.v7.widget.Toolbar
            android:id="@+id/MyToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

        <RelativeLayout
            android:layout_width="match_parent"
            app:layout_anchor="@+id/MyAppbar"
            android:layout_height="match_parent">
            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profile_image"
                android:layout_width="96dp"
                android:layout_height="96dp"
                android:src="@drawable/studentprofile"
                app:civ_border_width="2dp"
                app:civ_border_color="#0adcf4"
                android:layout_marginTop="58dp"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:id="@+id/branch"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:text="Branch:  "
                android:textColor="@color/textColorPrimary"
                android:textSize="14sp"
                android:layout_marginBottom="20sp"
                android:layout_alignParentBottom="true"
                android:layout_centerInParent="true" />
        </RelativeLayout>
    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

我的java代码是...

collapsingToolbar =
            (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);
    collapsingToolbar.setTitle(name);
    collapsingToolbar.setExpandedTitleTextAppearance(R.style.expandedappbar);
    collapsingToolbar.setCollapsedTitleTextAppearance(R.style.collapsedappbar);
    final ImageView profile=(ImageView)findViewById(R.id.profile_image);
    Picasso.with(this)
            .load(photo_url)
            .placeholder(R.drawable.studentprofile)
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .into(new Target() {
                @Override
                public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
                /* Save the bitmap or do something with it here */
                    Palette.from(bitmap).generate(new Palette.PaletteAsyncListener() {

                        @Override
                        public void onGenerated(Palette palette) {
                            collapsingToolbar.setContentScrimColor(palette.getMutedColor(ContextCompat.getColor(context,R.color.colorPrimary)));
                            collapsingToolbar.setStatusBarScrimColor(palette.getMutedColor(ContextCompat.getColor(context,R.color.colorPrimaryDark)));
                        }
                    });
                    //Set it in the ImageView
                    profile.setImageBitmap(bitmap);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {
                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
                }
            });

最佳答案

由于我自己解决了我的问题,所以我发布了这个答案,以便对其他人有所帮助。 CollapsingToolBar 扩展了 FrameLayout,因此在另一个标签之后编写的任何 xml 标签都将分层为父标签的顶部。我的 RelativeLayout 写在 Toolbar 之后,这就是为什么 Relative Layout 元素在 CollapsingToolbar< 之后显示在 Toolbar 崩溃了。 只需将 RelativeLayout 放在 Toolbar 之前,就会在工具栏折叠后隐藏该元素。

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapse_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    android:background="#ff009688"
    app:contentScrim="@color/colorPrimary"
    app:expandedTitleMarginTop="100sp"
    app:expandedTitleGravity="center|bottom"
    android:fitsSystemWindows="true">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        app:srcCompat="@drawable/bgimg"
        app:layout_collapseMode="parallax"/>

   <RelativeLayout
        android:layout_width="match_parent"
        app:layout_anchor="@+id/MyAppbar"
        android:layout_height="match_parent">
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_image"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:src="@drawable/studentprofile"
            app:civ_border_width="2dp"
            app:civ_border_color="#0adcf4"
            android:layout_marginTop="58dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/branch"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Branch:  "
            android:textColor="@color/textColorPrimary"
            android:textSize="14sp"
            android:layout_marginBottom="20sp"
            android:layout_alignParentBottom="true"
            android:layout_centerInParent="true" />
    </RelativeLayout>
    <android.support.v7.widget.Toolbar
        android:id="@+id/MyToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin" />

</android.support.design.widget.CollapsingToolbarLayout> 

关于java - Android折叠工具栏在折叠时没有隐藏其他元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41361579/

相关文章:

html - 导航栏折叠在平板电脑 View 中不起作用

java - 是否可以只覆盖接口(interface)中需要的抽象方法或 n 种方法中的抽象类?

java - 错误 "; expected",sql 语句

android - android 服务是否保证调用 onDestroy()?

android - 显示 fragment 时停止滚动 CollapsingToolbarLayout

android - 将滚动 fragment 设置为滚动 Activity

java - 如何批量读取重新投递队列?

Java 方法和泛型

java - android ndk 构建问题(..ld.exe : ./jni:没有这样的文件:权限被拒绝)

android - android 8.1.0安装burp证书后报"YOUR CONNECTION IS NOT PRIVATE"错误