Android頂部佈局View不隨著輸入框彈出而上移
阿新 • • 發佈:2019-02-05
前提
平時我們需要實現佈局底部佈局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” 屬性,否則不能達到預期效果。