java - 修复软键盘上方的布局
问题描述
我知道这个问题已经在互联网上流传了很长时间,但对我来说似乎没有任何效果。
怎么了
如屏幕截图所示,具有微调器的布局并未向上推,而是与软键盘重叠。我希望此布局在弹出时放置在键盘上方。
我的布局文件
<?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>
清单文件
<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>
输出
推荐阅读
- python - 我的代码在不应该循环的时候不断循环
- python - 使用 sklearn 重新缩放多元时间序列数据
- mysql - 无法弄清楚如何编写简单的 SQL 语句
- r - ggplot geom_linerange 中的 Alpha 由 Mac 上的观察次数确定
- java - OKTA 在点击 /groups 端点时返回 400 错误
- windows - 试图将超过 9 个文件拖放到批处理脚本中
- android - 我想要没有 MAIN 片段或按钮的条件登录导航
- postgresql - PostgreSQL 的统计收集器是否跟踪索引的*所有*使用情况?
- android - Kotlin android开发diffutil itemCallback类型错误
- neovim - 无法获取目录:“/home/myname/.config/nvim/init.vim”