java - 修复软键盘上方的布局

标签 java android xml android-layout

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

怎么了

enter image description here enter image description here

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

我的布局文件

<?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

最佳答案

试试这个

<?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>

list 文件

    <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>

输出

enter image description here enter image description here

关于java - 修复软键盘上方的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50987381/

相关文章:

java - 在没有控制台的情况下启动 java 程序

java - 如何在eclipse中引用android框架资源?

c - 如何在不使用 docptr 的情况下将 CDATA 作为值添加到特定节点

xml - 使用 XSL 计算 XML 中的端点

java - 如何打开安卓快速通知设置

java - 为什么 Java EE 是可扩展的?

java - 如何将ArrayList从AsyncTask返回到另一个类?

android - 如何将数据库值获取到android(sqlite数据库)中的字符串数组

android - 推特用户界面控件

java - 如何在 Java 中将 xsl 应用到 xml