Android Studio - 滚动视图在键盘打开时移到屏幕外

Android Studio - Scrollview moves outside the screen on keyboard open

我想制作一个聊天屏幕,底部有一个 EditText 和一个 Button(包装在 LinearLayout 中),顶部有一个用于消息的 ScrollView。问题在于,当用户点击 EditText 并且键盘打开时,ScrollView 将由于向上移动而离开屏幕。我想将布局(带有 EditBox 和按钮)移动到键盘上方,并调整 ScrollView 的大小以适合屏幕。问题如下图所示:

如您所见,写"Chat"的那一行(左图)在右图中消失了。

我使用这个 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="15sp"
    android:orientation="vertical"
    android:background="#ccdcf7"
    tools:context="com.orionz.sockettest.ChatActivity">

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="15"
        android:isScrollContainer="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/chatBox"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="5dp"
                android:text="Chat\n"
                android:textColor="#000000"
                android:textIsSelectable="true"
                android:textSize="22dp" />
        </LinearLayout>
    </ScrollView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/msgInput"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.8"
            android:ems="10"
            android:hint="Message..."
            android:inputType="text" />

        <Button
            android:id="@+id/sendButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="@drawable/mybutton"
            android:padding="5sp"
            android:text="Send"
            android:textColor="#ffffff"
            android:textSize="20sp" />

    </LinearLayout>

</LinearLayout>

我看到很多答案,例如设置 android:windowSoftInputMode 但 none 有效。我使用 Android Studio。任何帮助将不胜感激。

将 Scrollview 包裹到整个布局。

即将 editText 和按钮放在 Scrollview 中并将 scrollView 高度设置为 android:layout_height="match_parent"

终于,我发现了我的问题所在。

  • 主题是全屏。在 activity 的风格中,我有这一行导致了问题:

<item name="android:windowFullscreen">true</item>

  • 在 java 代码中,我在 activity 的开头给出了打开键盘的命令:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

这也是问题的一部分

  • 将 adjustResize 属性 添加到清单中的 activity 后,一切正常:

android:windowSoftInputMode="adjustResize"

尝试用这样的 ListView 替换 ScrollView :

<ListView
    android:id="@+id/list_view"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1">
</ListView>

将所有消息存储在 ArrayList 中,并为您创建的 ListView 设置一个适配器:

ListView listView=(ListView) findViewById(R.id.list_view);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ArrayList);
listView.setAdapter(adapter);

最后,在你的 Button 上添加一个监听器,写下这些行:

EditText editText= (EditText) findViewById(R.id.msgInput);
ArrayList.add(editText.getText().toString());
adapter.notifyDataSetChanged();