风格为Theme.Dialog的Activity纯代码手写布局里scrollview的问题

本文详细介绍了如何通过设置Activity的windowSoftInputMode属性来解决软键盘弹出时遮挡Activity UI的问题。通过将属性设置为stateHidden|adjustResize,可以确保软键盘出现时UI布局自动调整,显示滚动条,提升用户体验。

风格为Theme.Dialog的Activity,即manifest里对Activity加上属性android:theme="@android:style/Theme.Dialog",

 

如果Activity里的所有布局是纯代码所写,不是从layout xml里生成,布局包含一个ScrollView,ScrollView里有很多TextView, EditText这类的小widget, 当用户点击其中一个EditText时,软键盘弹出来盖住了Activity的UI,Activity的UI没有自动缩小显示出scrollview的滚动条, 这是为什么呢?

 

这跟Activity的一个属性有关,即android:windowSoftInputMode:

 

这里设置其为 android:windowSoftInputMode=“stateHidden|adjustResize”即可解决上述我们的问题!

 

具体是什么原因,看以下SDK文档解释就明白了:

 

How the main window of the activity interacts with the window containing the on-screen soft keyboard. The setting for this attribute affects two things:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

The setting must be one of the values listed in the following table, or a combination of one "state... " value plus one "adjust... " value. Setting multiple values in either group — multiple "state... " values, for example — has undefined results. Individual values are separated by a vertical bar (| ). For example:

 

<activity
 
android:windowSoftInputMode
=
"stateVisible|adjustResize"
 . . . 
>

 

Values set here (other than "stateUnspecified " and "adjustUnspecified ") override values set in the theme.

 

ValueDescription
"stateUnspecified "The state of the soft keyboard (whether it is hidden or visible) is not specified. The system will choose an appropriate state or rely on the setting in the theme.

This is the default setting for the behavior of the soft keyboard.

"stateUnchanged "The soft keyboard is kept in whatever state it was last in, whether visible or hidden, when the activity comes to the fore.
"stateHidden "The soft keyboard is hidden when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.
"stateAlwaysHidden "The soft keyboard is always hidden when the activity's main window has input focus.
"stateVisible "The soft keyboard is visible when that's normally appropriate (when the user is navigating forward to the activity's main window).
"stateAlwaysVisible "The soft keyboard is made visible when the user chooses the activity — that is, when the user affirmatively navigates forward to the activity, rather than backs into it because of leaving another activity.
"adjustUnspecified "It is unspecified whether the activity's main window resizes to make room for the soft keyboard, or whether the contents of the window pan to make the currentfocus visible on-screen. The system will automatically select one of these modes depending on whether the content of the window has any layout views that can scroll their contents. If there is such a view, the window will be resized, on the assumption that scrolling can make all of the window's contents visible within a smaller area.

This is the default setting for the behavior of the main window.

"adjustResize "The activity's main window is always resized to make room for the soft keyboard on screen.
"adjustPan "The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值