我正在用自定义视图在android中折叠工具栏。

时间:2018-08-21 11:12:52

标签: android android-layout android-fragments

工具栏的折叠功能运行良好,但效果非常差,并且在展开和折叠时会抖动。下面是我的布局代码。甚至有时工具栏会变得很大。我在此代码中使用了自定义视图而不是图像。定制视图是约束布局。片段内的特定代码和片段由其中具有recyclerview的选项卡布局组成。我已经在自定义视图中使用了视差效果,所以对此不太确定。

    <android.support.design.widget.CoordinatorLayout
        tools:context=".ProfileFragment"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical">

        <!--this is the collapse mode-->
        <android.support.design.widget.AppBarLayout
            android:id="@+id/htab_appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="1dp">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="304dp"
                android:background="@color/white"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorAccent"
                app:expandedTitleTextAppearance="@style/TransparentText"
                app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
                app:title="paco">

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_collapseMode="parallax"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/parentLayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"
                    >


                    <!-- TODO: Update blank fragment layout -->

                    <ImageView
                        android:id="@+id/profileImage"
                        android:layout_width="87dp"
                        android:layout_height="87dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="24dp"
                        android:contentDescription="Profile Image"
                        android:src="@drawable/capture"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <ImageView
                        android:id="@+id/editImage"
                        android:layout_width="16dp"
                        android:layout_height="16dp"
                        android:layout_marginLeft="100dp"
                        android:layout_marginStart="100dp"
                        android:contentDescription="TODO"
                        app:layout_constraintStart_toEndOf="@+id/profileImage"
                        app:layout_constraintTop_toTopOf="@+id/profileImage"
                        app:srcCompat="@drawable/ic_baseline_launch_24px" />

                    <TextView
                        android:id="@+id/artistName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="7dp"
                        android:text="Paco Pardesi"
                        android:textColor="#333333"
                        android:textSize="14sp"
                        app:layout_constraintEnd_toEndOf="@+id/profileImage"
                        app:layout_constraintStart_toStartOf="@+id/profileImage"
                        app:layout_constraintTop_toBottomOf="@+id/profileImage" />

                    <TextView
                        android:id="@+id/country"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:text="India"
                        android:textColor="#757575"
                        android:textSize="14sp"
                        app:layout_constraintEnd_toEndOf="@+id/artistName"
                        app:layout_constraintStart_toStartOf="@+id/artistName"
                        app:layout_constraintTop_toBottomOf="@+id/artistName" />

                    <TextView
                        android:id="@+id/descriptionArtist"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="2dp"
                        android:layout_marginLeft="2dp"
                        android:layout_marginRight="2dp"
                        android:layout_marginStart="2dp"
                        android:layout_marginTop="12dp"
                        android:text="Artist from the paradise on earth, Kashmir. I like to create art that is about freedom and fresh thinking."
                        android:textColor="#323232"
                        android:textSize="13sp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/country" />

                    <android.support.constraint.ConstraintLayout
                        android:id="@+id/textviewLayout"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="48dp"
                        android:layout_marginLeft="48dp"
                        android:layout_marginRight="48dp"
                        android:layout_marginStart="48dp"
                        android:layout_marginTop="17dp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/descriptionArtist">

                        <TextView
                            android:id="@+id/uploadCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="1233"
                            android:textColor="#bea57c"
                            android:textSize="14sp"
                            app:layout_constraintEnd_toEndOf="@+id/Uploads"
                            app:layout_constraintStart_toStartOf="@+id/Uploads"
                            app:layout_constraintTop_toTopOf="parent" />

                        <TextView
                            android:id="@+id/Uploads"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="32dp"
                            android:layout_marginStart="32dp"
                            android:text="Uploads"
                            android:textColor="#333333"
                            android:textSize="14sp"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintTop_toBottomOf="@+id/uploadCount" />

                        <TextView
                            android:id="@+id/createdCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="4567"
                            android:textColor="#bea57c"
                            android:textSize="14sp"
                            app:layout_constraintEnd_toEndOf="@+id/created"
                            app:layout_constraintStart_toStartOf="@+id/created"
                            app:layout_constraintTop_toTopOf="parent" />

                        <TextView
                            android:id="@+id/created"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="32dp"
                            android:layout_marginStart="32dp"
                            android:text="Created"
                            android:textColor="#333333"
                            android:textSize="14sp"
                            app:layout_constraintStart_toEndOf="@+id/Uploads"
                            app:layout_constraintTop_toBottomOf="@+id/createdCount" />

                        <TextView
                            android:id="@+id/likesCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="8000"
                            android:textColor="#bea57c"
                            android:textSize="14sp"
                            app:layout_constraintEnd_toEndOf="@+id/likes"
                            app:layout_constraintStart_toStartOf="@+id/likes"
                            app:layout_constraintTop_toTopOf="parent" />

                        <TextView
                            android:id="@+id/likes"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginEnd="32dp"
                            android:layout_marginLeft="32dp"
                            android:layout_marginRight="32dp"
                            android:layout_marginStart="32dp"
                            android:text="Likes"
                            android:textColor="#333333"
                            android:textSize="14sp"
                            app:layout_constraintEnd_toEndOf="parent"
                           app:layout_constraintStart_toEndOf="@+id/created"
                       app:layout_constraintTop_toBottomOf="@+id/likesCount" />

                    </android.support.constraint.ConstraintLayout>

                </android.support.constraint.ConstraintLayout>
                </FrameLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:alpha="0"
                    android:background="@android:color/black"
                    android:fitsSystemWindows="true"/>

                <android.support.v7.widget.Toolbar
                    android:id="@+id/htab_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_gravity="top"
                    android:layout_marginBottom="48dp"
                    app:layout_collapseMode="pin"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

                <android.support.design.widget.TabLayout
                    android:id="@+id/result_tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@color/colorNormal"
                    app:tabBackground="@drawable/tab_selector"
                    app:tabIndicatorColor="#f6f6f6"
                    app:tabIndicatorHeight="0dp"
                    app:tabMode="fixed"
                    app:tabTextColor="#333333" />
            </android.support.design.widget.CollapsingToolbarLayout>

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

        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />
    </android.support.design.widget.CoordinatorLayout>

1 个答案:

答案 0 :(得分:0)

尝试将CollapsingToolbarLayout中当前存在的所有自定义组件添加到单独的布局文件中。然后使用 <include layout="@layout/my_custom_layout"/>

不知道为什么,但是以某种方式为我工作。看看这是否适合您。