前提
平时我们需要实现布局底部布局View随着输入框弹出而上移的效果,但是有些时候我们需要实现顶部布局View不随着输入框弹出而上移,比如自定义的Activity的title,这个时候就不希望随着输入框弹出而title也上移。以为此时title上移就看不见了。所以我们需要实现顶部title View不随着输入框的弹出而上移。
实现的主要代码是布局文件,实现如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="此处为顶部固定View,不随着输入框弹出而上移"
android:textColor="@android:color/white"
android:textSize="20sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="此处为内容区域,随着输入框弹出而上移"
android:textSize="20sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="400dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
解析:
- 把不随着输入框弹出而上移的View布局不放在ScrollView中
- 把需要随着输入框弹出而上移的View布局放在ScrollView中
注意:ScrollView 不能添加 android:scrollbars=”none” 属性,否则不能达到预期效果。