【问题标题】:Varying windowSoftInputMode for fragments inside the viewpager为 viewpager 内的片段改变 windowSoftInputMode
【发布时间】:2016-03-04 12:48:27
【问题描述】:

我有一个带有 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);

我已将主题自定义为:

<style name="NoResize" parent="@style/AppTheme">
    <item name="android:windowSoftInputMode">adjustPan</item>
</style>

活动是使用默认的 windowSoftInputMode 定义的,当软键盘打开时会调整视图的大小。

将一直在解决这个问题,直到它得到解决,但如果其他人有这个问题并解决了它,很高兴听到任何想法。

谢谢!

【问题讨论】:

  • 找到解决办法了吗?

标签: android


【解决方案1】:

您可以在每个 onCreateView 上调用

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

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

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

【讨论】:

  • Moxor,我不太明白您遇到了什么问题,但您可能希望打开自己的问题以便更详细地描述它。对于此处提出的问题-更改窗口的软输入模式-您的答案是正确的。可能有像您这样的情况,窗口的软输入模式不够用,但没有针对所有此类情况的单一解决方案;这将取决于您的特定问题的性质。
  • 谢谢,我可能会提出一个新问题。我的特殊问题是同时有两个可见的片段,并且期望每个片段都有不同的行为
  • 似乎只有在您明确定义 AndroidManifest 中的任何值后,此方法才开始工作。 android:windowSoftInputMode="adjustPan" 此外,这个值应该不同于adjustUnspecified。如果没有这个明确的定义,setSoftInputMode(...) 方法将不起作用。
猜你喜欢
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 2012-04-04
相关资源
最近更新 更多