对viewpager中的片段改变windowSoftInputMode

Cal*_*ark 39 android

我有一个包含2个不同片段的ViewPager.对于第一个片段,我想定义它,以便它不会在软键盘打开时调整大小.对于第二个片段,我想让它调整大小.

设置android:windowSoftInputMode="adjustPan"清单内部将适用于两个片段,但我想在两者之间进行更改.

谷歌搜索后我做了什么:

    // create ContextThemeWrapper from the original Activity Context with the custom theme
    Context context = new ContextThemeWrapper(getActivity(), R.style.NoResize);
    // clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(context);
    // inflate using the cloned inflater, not the passed in default
    return localInflater.inflate.inflate(R.layout.my_layout,container,false);
Run Code Online (Sandbox Code Playgroud)

我自定义了主题:

<style name="NoResize" parent="@style/AppTheme">
    <item name="android:windowSoftInputMode">adjustPan</item>
</style>
Run Code Online (Sandbox Code Playgroud)

使用默认的windowSoftInputMode定义活动,该软件会在软键盘打开时调整视图大小.

将会解决这个问题,直到它得到解决,但如果其他人有这个问题并解决了它,那么听到任何想法都会很棒.

谢谢!

Mox*_*xor 75

您可以调用每个onCreateView

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Run Code Online (Sandbox Code Playgroud)

检查http://developer.android.com/reference/android/view/WindowManager.LayoutParams.html#SOFT_INPUT_ADJUST_RESIZE

在我的情况下,这并没有解决问题,因为我有一个透明的片段(一个聊天窗口),我需要调整大小,底部的片段不应该.但我认为这对你有用.

  • 似乎只有在您在 AndroidManifest 中明确定义任何值后,此方法才开始工作。`android:windowSoftInputMode="adjustPan"` 此外,这个值应该与 `adjustUnspecified` 不同。如果没有这个明确的定义,`setSoftInputMode(...)` 方法将不起作用。 (6认同)
  • 谢谢,我可能会提出一个新问题.我的特殊问题是同时有两个可见的碎片,并期望每个碎片有不同的行为 (3认同)