View 浮在软键盘上多种实现方式及踩坑
2017-05-01 20:45
281 查看
做andriod 开发的都知道当我们需要view 浮在软键盘上最简单的做法就是使用ScrollView 将所需要浮起的View 包裹起来,然后利用ScrollView 与软键盘的特性就能实现浮起来的效果。
然而还是有不同的形式的,及坑要踩。本文就简单介绍下
然而现在有个需求,当出现软键盘时,提交的按钮button浮在软键盘上,下面的这个解决办法就不行了,只能满足EditText在软键盘上,软键盘可能会遮挡button.
需求如下:
就是在ScrollView 上添加
就能实现把button 浮在软键盘上,将布局顶上去效果。
注:这个实现方式会导致顶上去的View 在软键盘还在的情况下不能将顶部内容滑动下来。
注:然而这个有个条件,就是获得焦点的EditText不能比键盘低,否则button 浮不起来,实现不了我们需要的效果
另外记得用完软键盘后将软键盘收起来,特别是在同一activity内有多个fagment,有的fragment 出现软键盘,有的页面不需要情况下,会发生相互干扰布局的情况,所以一定做好软键盘的回收工作。
当然如果需要键盘影响布局可以设置
当然关于软键盘的模式,各位可以去google 下软键盘的windowSoftInputMode,笔者就再这里赘述了。
然而当我们去掉
那我们能不能不用这个属性windowIsFloating,还能实现dialog 类似的样式,解决掉button 不能浮在软键盘上的问题呢?答案是有的。
我们可以采用上面的主题,实现一个仿dialog 样式的activity。
然而还是有不同的形式的,及坑要踩。本文就简单介绍下
一. ScrollView 窗口上移
软键盘浮起时布局变化与占据焦点的EditText位置有很大关系。当占据焦点的EditText在软键盘上面布局不会变动,否则会调整布局,将占据焦点的View 顶上去。然而现在有个需求,当出现软键盘时,提交的按钮button浮在软键盘上,下面的这个解决办法就不行了,只能满足EditText在软键盘上,软键盘可能会遮挡button.
需求如下:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#FF33B5E5" android:gravity="center" android:text="Content" android:textColor="@android:color/white" android:textSize="50sp" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:text="EditText" /> <Button android:layout_width="match_parent" android:background="#00f" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
变体1
我们实现变体1:<ScrollView android:layout_width="match_parent" android:layout_gravity="bottom" android:layout_weight="1" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="bottom" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#FF33B5E5" android:gravity="center" android:text="Content" android:textColor="@android:color/white" android:textSize="50sp" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:text="EditText" /> <Button android:layout_width="match_parent" android:background="#00f" android:layout_height="wrap_content" /> </LinearLayout> </ScrollView>
就是在ScrollView 上添加
android:gravity="bottom",及里面包裹的LinearLayout 添加
android:layout_gravity="bottom"
就能实现把button 浮在软键盘上,将布局顶上去效果。
注:这个实现方式会导致顶上去的View 在软键盘还在的情况下不能将顶部内容滑动下来。
变体2:
当然如果你将Button放在ScrollView外面的时候也可以,也会将Button 浮在软键盘上面,这时ScrollView 包裹的LinearLayout 的android:layout_gravity="bottom"就得去掉,此时ScrollView中内容可以向上向下滑动,可以看到完整内容。
<ScrollView android:layout_width="match_parent" android:layout_gravity="bottom" android:layout_weight="1" android:fillViewport="true" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="200dp" android:background="#FF33B5E5" android:gravity="center" android:text="Content" android:textColor="@android:color/white" android:textSize="50sp" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:text="EditText" /> </LinearLayout> </ScrollView> <Button android:layout_width="match_parent" android:background="#00f" android:layout_height="wrap_content" />
变体3:
如果仅仅要求Button 浮在软键盘上,可以用ScrollView 包裹Button,代码如下:注:然而这个有个条件,就是获得焦点的EditText不能比键盘低,否则button 浮不起来,实现不了我们需要的效果
<TextView android:layout_width="match_parent" android:layout_height="100dp" android:background="#FF33B5E5" android:gravity="center" android:text="Content" android:textColor="@android:color/white" android:textSize="50sp" /> <EditText android:layout_width="match_parent" android:layout_height="50dp" android:text="EditText" /> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:fillViewport="true"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#00f" /> </FrameLayout> </ScrollView>
二 软键盘浮起View 踩坑
1. 软键盘对布局的影响
当软键盘出现的时候对scrollView 及LinearLayout 等布局是有影响的,当一个activity 软键盘没有及时收回,也会对下一个页面的布局会有影响。如果下一页面没有EdtText 不想软键盘对布局造成影响,可以给activity设置android:windowSoftInputMode="stateAlwaysHidden|adjustPan" ``` 或者在代码里面写 ``` getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
另外记得用完软键盘后将软键盘收起来,特别是在同一activity内有多个fagment,有的fragment 出现软键盘,有的页面不需要情况下,会发生相互干扰布局的情况,所以一定做好软键盘的回收工作。
当然如果需要键盘影响布局可以设置
android:windowSoftInputMode="adjustResize"
当然关于软键盘的模式,各位可以去google 下软键盘的windowSoftInputMode,笔者就再这里赘述了。
2. View 不能浮在软键盘上
当activity 主题设置为<item name="android:windowIsFloating">true</item>即activity 浮在其他页面上(类似dialog),或者直接给页面设置dialog 属性时,我们采取各种方法让button 浮在软键盘上,在部分手机上都无效,笔者在 meizu Note2 Android 5.1 发现这个问题。
然而当我们去掉
<item name="android:windowIsFloating">true</item>时,奇迹发生了,button可以浮起来了。可见还是因为这个属性导致button 不能浮在软键盘上(dialog主题 内部已经设置windowIsFloating 为ture)
那我们能不能不用这个属性windowIsFloating,还能实现dialog 类似的样式,解决掉button 不能浮在软键盘上的问题呢?答案是有的。
<style name="FloatingTheme" parent="AppTheme"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:backgroundDimEnabled">true</item> </style>
我们可以采用上面的主题,实现一个仿dialog 样式的activity。
相关文章推荐
- RecyclerView实现多种Item,最优雅的方式,getItemViewType(以即时通讯app为例子)
- [置顶] 实现CirleImageView圆形图片的多种方式
- ViewPager的实现过程及其多种方式的动画效果
- ASP.NET MVC传递Model到视图的多种方式总结(二)__关于ViewBag、ViewData和TempData的实现机制与区别
- matlab实现gabor filter 多种方式汇总
- 关于ViewPager、ViewFilpper、ViewFlow三种实现水平向滑动方式的比较
- 多种方式实现Android页面布局的切换
- C#对象的浅拷贝,深拷贝及利用序列化等多种方式实现深拷贝
- 生产者/消费者问题的多种Java实现方式
- 生产者/消费者问题的多种Java实现方式
- 关于ViewPager、ViewFilpper、ViewFlow三种实现水平向滑动方式的比较
- javascript实现继承的多种方式
- 生产者/消费者问题的多种Java实现方式
- 多种方式实现ul中的li居中的利弊
- 关于ViewPager、ViewFilpper、ViewFlow三种实现水平向滑动方式的比较
- ((ios开发学习笔记 十一))自定义TableViewCell 的方式实现自定义TableView(带源码)
- Android:View Animition 两种实现方式
- 禁止用户重复登陆的多种实现方式
- Android之ImageView的加边框颜色的几种实现方式
- Hibernate关联查询有多种实现方式