简体   繁体   English

设置 RelativeLayout 的高度以匹配软键盘的高度

[英]Set height of RelativeLayout to match height of Soft Keyboard

I need to set the height of this RelativeLayout to match the height of the Soft Keyboard .我需要设置此RelativeLayout的高度以匹配Soft Keyboard的高度。 To make sure the EditText appears just above the keyboard.确保EditText出现在键盘上方。

Is it possible to set some kind of Listener to detect every time the keyboard height changes?是否可以设置某种侦听器来检测每次键盘高度变化?

在此处输入图像描述

The proper way to do this would be to set soft input to match your needs.执行此操作的正确方法是设置软输入以满足您的需要。 try following for a fragment put this line in your fragment's onCreate method:尝试按照片段将此行放在片段的 onCreate 方法中:

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

You can set multiple modes with a single line like您可以使用一行设置多种模式,例如

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

And if you are working in an activity you can just adjust it in your android manifest file for each activity如果您正在进行一项活动,您只需在 android 清单文件中为每个活动进行调整

<activity
    android:name=".SomeActivity"
    android:windowSoftInputMode="adjustResize" />

or android:windowSoftInputMode="adjustPan|adjustResize" Check which settings suits your needs better between adjustPan, adjustUnspesified, adjustResize or some other soft input options.android:windowSoftInputMode="adjustPan|adjustResize"检查 adjustPan、adjustUnspesified、adjustResize 或其他一些软输入选项之间哪些设置更适合您的需求。

I figured out a solution that seems to work fine.我想出了一个似乎工作正常的解决方案。 With only 1 problem.只有 1 个问题。 For some reason the Status Bar turns white with this piece of code.. Any idea why?出于某种原因, Status Bar会随着这段代码变白。知道为什么吗?

ViewCompat.setOnApplyWindowInsetsListener(this.getWindow().getDecorView(), (v, insets) -> {
        
    int keyboardHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom;
        
    //Code logic
        
    return insets;
});

How I set the Status Bar color:我如何设置Status Bar颜色:

<style name="AppTheme.GuessWord">
    <item name="colorPrimaryDark">@color/primaryColor</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM