Android顶部布局View不随着输入框弹出而上移

前提

平时我们需要实现布局底部布局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>

解析:

  1. 把不随着输入框弹出而上移的View布局不放在ScrollView中
  2. 把需要随着输入框弹出而上移的View布局放在ScrollView中

注意:ScrollView 不能添加 android:scrollbars=”none” 属性,否则不能达到预期效果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值