Android Studio实现头部固定悬浮、ScrollView滚动内容位于头部下方的方法
问题原因
你当前使用RelativeLayout作为根布局时,子控件的绘制层级和代码书写顺序一致,ScrollView写在头部布局的后面,所以层级比头部更高,滚动时内容自然会覆盖头部区域。同时你的ScrollView没有设置相对头部的位置约束,默认会从根布局顶部开始绘制,也是覆盖问题的诱因之一。
解决方案
推荐使用更简单的垂直LinearLayout作为根布局实现固定头部效果,不需要处理复杂的相对约束,修改后的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" tools:context=".Fragment.RecordFragment" android:orientation="vertical"> <!-- 头部固定区域 包裹原来的头部控件 --> <RelativeLayout android:id="@+id/header_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <View android:layout_width="369dp" android:layout_height="71dp" android:layout_alignParentLeft="true" android:layout_marginLeft="13dp" android:layout_alignParentTop="true" android:layout_marginTop="14dp" android:background="@drawable/ic_header"/> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginStart="25dp" android:layout_marginTop="25dp" android:layout_marginEnd="25dp" android:layout_marginBottom="25dp" android:text="Record" android:textAlignment="center" android:textColor="@color/white" android:textSize="35sp" /> </RelativeLayout> <!-- 滚动区域 占满剩余高度 --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" android:paddingBottom="100dp"> <TextView android:id="@+id/txtDate" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="Date for today" android:textAlignment="center" android:textColor="@color/black" android:textSize="50sp" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <View android:layout_width="316dp" android:layout_height="201dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_gravity="center" android:layout_marginTop="50dp" android:background="@drawable/sec_bp" /> <EditText android:layout_width="250dp" android:layout_height="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="70dp" android:background="@drawable/rounded_corner" android:gravity="center" android:hint="SBP" /> <EditText android:layout_width="250dp" android:layout_height="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="120dp" android:background="@drawable/rounded_corner" android:gravity="center" android:hint="DBP" /> <EditText android:layout_width="250dp" android:layout_height="30dp" android:layout_marginLeft="30dp" android:layout_marginTop="170dp" android:background="@drawable/rounded_corner" android:gravity="center" android:hint="BPM" /> <Button android:layout_width="70dp" android:layout_height="30dp" android:layout_marginLeft="120dp" android:layout_marginTop="210dp" android:background="@drawable/rounded_corner" android:gravity="center" android:text="save" /> </RelativeLayout> <View android:layout_width="316dp" android:layout_height="117dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="50dp" android:background="@drawable/sec_temp" /> <View android:layout_width="316dp" android:layout_height="117dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="50dp" android:background="@drawable/sec_temp" /> </LinearLayout> </ScrollView> </LinearLayout>
补充说明
如果要保留原有的RelativeLayout根布局,只需要给头部控件包裹的布局设置id,再给ScrollView添加android:layout_below="@id/header_layout"属性,同时将ScrollView高度改为match_parent即可实现相同效果。
内容的提问来源于stack exchange,提问作者MeltedIceCream
相关产品推荐
相关产品推荐

