windowSoftInputMode 在fragment中使用
时间: 2023-08-19 16:29:47 浏览: 71
在 Fragment 中使用 windowSoftInputMode 属性时,需要在 Fragment 的 onCreateView 方法中获取当前 Fragment 的根布局,并设置根布局的 windowSoftInputMode 属性。
具体步骤如下:
1. 在 Fragment 的 onCreateView 方法中获取根布局:
```java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my, container, false);
return rootView;
}
```
2. 获取根布局的窗口属性,并设置 windowSoftInputMode 属性为 adjustPan:
```java
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
}
```
需要注意的是,当 Fragment 所在的 Activity 中有多个 Fragment 时,使用 adjustPan 属性可能会导致多个 Fragment 之间的布局重叠问题。这时可以使用 adjustResize 属性,自动调整当前 Activity 的主窗口大小,以保证软键盘与输入框不重叠。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)