我有一个带有 2 个不同 fragment 的 ViewPager。 对于第一个 fragment ,我想对其进行定义,使其不会在软键盘打开时调整大小。 对于第二个 fragment ,我想让它调整大小。
在 list 中设置 android:windowSoftInputMode="adjustPan"
将适用于两个 fragment ,但我想在两者之间改变它。
谷歌搜索后我做了什么:
// 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>
Activity 是使用默认的 windowSoftInputMode 定义的,当软键盘打开时会调整 View 的大小。
将一直在解决这个问题,直到它得到解决,但如果其他人有这个问题并解决了它,很高兴听到任何想法。
谢谢!
最佳答案
您可以在每个 onCreateView 上调用
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
在我的情况下,这并不能解决问题,因为我有一个透明的 Fragment(一个聊天窗口),我需要调整它的大小,而底部的 Fragment 不应该。但我认为它可能对你有用。
关于android - 为 viewpager 中的 fragment 改变 windowSoftInputMode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16421857/