首页 > 解决方案 > 修复软键盘上方的布局

问题描述

我知道这个问题已经在互联网上流传了很长时间,但对我来说似乎没有任何效果。

怎么了

在此处输入图像描述 在此处输入图像描述

如屏幕截图所示,具有微调器的布局并未向上推,而是与软键盘重叠。我希望此布局在弹出时放置在键盘上方。

我的布局文件

<?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:background="@android:color/white"
    android:orientation="vertical"
    tools:context="believe.cht.speak.MainActivity">

    <ImageView
        android:id="@+id/logo"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_gravity="center_horizontal"
        android:layout_margin="16dp"
        android:src="@drawable/ic_bubble"
        android:tint="@color/blue" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/transparent"
        android:gravity="top|start"
        android:hint="Start typing..."
        android:padding="8dp" />

    <ImageView
        android:id="@+id/shadow"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:src="@drawable/shadow" />

    <RelativeLayout
        android:id="@+id/spinnerLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="@color/blue">

        <Spinner

            android:id="@+id/spinner"
            android:layout_width="200dp"
            android:layout_height="48dp"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</LinearLayout>

我试过的

android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustPan" //in the manifest, also tried combining the two

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); //in the activity

<item name="android:windowActionBarOverlay">true</item> //in styles.xml

标签: javaandroidxmlandroid-layout

解决方案


尝试这个

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/logo"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_gravity="center_horizontal"
            android:layout_margin="16dp"
            android:src="@drawable/ic_message" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:gravity="top|start"
            android:hint="Start typing..."
            android:padding="8dp" />

        <ImageView
            android:id="@+id/shadow"
            android:layout_width="match_parent"
            android:layout_height="6dp"
            android:src="@drawable/ic_camera" />

        <RelativeLayout
            android:id="@+id/spinnerLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@color/colorPrimary">

            <Spinner

                android:id="@+id/spinner"
                android:layout_width="200dp"
                android:layout_height="48dp"
                android:layout_alignParentEnd="true" />

        </RelativeLayout>

    </LinearLayout>
</ScrollView>

清单文件

    <activity android:name=".MyActivity"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

输出

在此处输入图像描述 在此处输入图像描述


推荐阅读