无ScrollView时布局自动滚动,Toolbar无法固定的修复方案咨询
Hey there, let's fix that stubborn Toolbar that won't stay put! The issue comes down to a few layout misconfigurations in your RelativeLayout setup—let's break it down and adjust your code step by step.
What's Causing the Problem?
- Your Toolbar isn't explicitly pinned to the top of the parent layout, so it gets pushed around when other views scroll.
- The top
ConstraintLayoutusesmatch_parentfor height, which makes it cover the entire screen (including behind the Toolbar) and messes up the layout hierarchy. - The TabLayout uses a fixed
marginTopinstead of relying on layout dependencies, which makes its position unstable. - The RecyclerView's layout isn't properly constrained to sit below the TabLayout, so its scroll behavior affects the entire layout.
Fixed Layout Code
Here's the adjusted XML with all the necessary changes to keep your Toolbar fixed at the top:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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_width="match_parent" android:layout_height="match_parent" tools:context="com.example.user.app.Utils.ViewProfileFragment"> <!-- Toolbar pinned to top --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar01" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorS50Alpha" android:elevation="2dp" android:minHeight="?attr/actionBarSize" android:theme="?attr/actionBarTheme" android:layout_alignParentTop="true" /> <!-- This pins it to the top --> <!-- Top profile section, now below Toolbar with wrap_content height --> <android.support.constraint.ConstraintLayout android:id="@+id/profileHeaderLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/toolbar01"> <!-- Ties it directly to the Toolbar --> <ImageView android:id="@+id/ivProfileBackphoto" android:layout_width="0dp" android:layout_height="320dp" android:background="@drawable/background_signinandregister" android:scaleType="centerCrop" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:srcCompat="@color/colorProfileBack" /> <RelativeLayout android:id="@+id/relativeLayout5" android:layout_width="0dp" android:layout_height="150dp" android:background="@drawable/shadow_profile_01" app:layout_constraintBottom_toBottomOf="@+id/ivProfileBackphoto" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/ivProfilephoto" android:layout_width="160dp" android:layout_height="160dp" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:scaleType="centerCrop" app:layout_constraintBottom_toBottomOf="@+id/ivProfileBackphoto" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.361" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginTop="8dp" android:fontFamily="sans-serif" android:text="Foster The People" android:textAppearance="@style/TextAppearance.AppCompat" android:textColor="@android:color/white" android:textSize="30sp" app:layout_constraintBottom_toBottomOf="@+id/ivProfileBackphoto" app:layout_constraintHorizontal_bias="0.497" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/ivProfilephoto" app:layout_constraintVertical_bias="0.0" /> <TextView android:id="@+id/tvDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginHorizontal="40dp" android:layout_marginTop="1dp" android:text="I like bacon and cheese sandwiches" android:textAlignment="center" android:textColor="@color/colorGrey" android:textSize="16sp" app:layout_constraintBottom_toBottomOf="@+id/ivProfileBackphoto" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/tvName" app:layout_constraintVertical_bias="0.0" /> <ImageView android:id="@+id/ivAddFriend" android:layout_width="60dp" android:layout_height="60dp" android:layout_margin="8dp" app:layout_constraintBottom_toBottomOf="@+id/relativeLayout5" app:layout_constraintRight_toLeftOf="@+id/ivProfilephoto" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.418" app:srcCompat="@drawable/addfriend02" /> <ImageView android:id="@+id/ivRemoveFriend" android:layout_width="60dp" android:layout_height="60dp" android:layout_margin="8dp" app:layout_constraintBottom_toBottomOf="@+id/relativeLayout5" app:layout_constraintRight_toLeftOf="@+id/ivProfilephoto" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.418" app:srcCompat="@drawable/removefriend02" /> </android.support.constraint.ConstraintLayout> <!-- TabLayout now below the profile header --> <android.support.design.widget.TabLayout android:id="@+id/tbProfiletabs" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@+id/profileHeaderLayout" <!-- Tied to the header --> android:background="@color/colorS50Alpha" app:tabSelectedTextColor="@android:color/white" app:tabTextColor="@color/colorSearch"> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Left" /> <android.support.design.widget.TabItem android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Center" /> </android.support.design.widget.TabLayout> <!-- RecyclerView now only scrolls below the TabLayout --> <android.support.v7.widget.RecyclerView android:id="@+id/lvProfilePosts" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tbProfiletabs" android:visibility="visible" /> <!-- If you need the ViewPager, uncomment and adjust its layout to sit below TabLayout --> <!-- <android.support.v4.view.ViewPager android:id="@+id/profileTabsViewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/tbProfiletabs"> </android.support.v4.view.ViewPager> --> </RelativeLayout>
Key Changes Explained
- Added
android:layout_alignParentTop="true"to the Toolbar to lock it to the top of the screen. - Changed the top
ConstraintLayoutto usewrap_contentheight andandroid:layout_below="@+id/toolbar01"so it sits directly under the Toolbar without covering it. - Removed the fixed
marginTopfrom the TabLayout and tied it to the profile header withandroid:layout_below="@+id/profileHeaderLayout". - Ensured the RecyclerView is constrained to sit below the TabLayout, so its scroll behavior stays contained to that area—no more pushing the Toolbar around!
If you still need the ViewPager, just uncomment it and make sure it's also set to android:layout_below="@+id/tbProfiletabs" with match_parent height (you'll probably want to use either RecyclerView or ViewPager, not both, to avoid layout overlap).
内容的提问来源于stack exchange,提问作者Dick Harris
相关产品推荐
相关产品推荐

