Android折叠工具栏在折叠时未隐藏其他元素 [英] Android Collapsing Toolbar was not hiding other element while collapsed

查看:40
本文介绍了Android折叠工具栏在折叠时未隐藏其他元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a layout on Android support design collapsing toolbar which contains the TextView but when I collapse my toolbar. some TextView showing with toolbar title. I want to hide every other things rather than toolbar and title. Here is my layout.

<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代码是...

And my java code is...

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之后,这就是CollapsingToolbar折叠后Relative Layout元素显示在Toolbar上的原因. 只需将RelativeLayout放在Toolbar之前,即可在工具栏折叠后隐藏该元素.

Since I resolve my problem myself I'm posting this answer so it will helpfull for others . CollapsingToolBar extends FrameLayout so any xml tag written after another tag will be layered as top of parent. My RelativeLayout is written after the Toolbar thats why Relative Layout elements are showing on Toolbar after CollapsingToolbar collapsed. just putting RelativeLayout before the Toolbar will hides the element after toolbar collapsed.

<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> 

这篇关于Android折叠工具栏在折叠时未隐藏其他元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆