您的位置:首页 > 移动开发 > Android开发

View 浮在软键盘上多种实现方式及踩坑

2017-05-01 20:45 281 查看
做andriod 开发的都知道当我们需要view 浮在软键盘上最简单的做法就是使用ScrollView 将所需要浮起的View 包裹起来,然后利用ScrollView 与软键盘的特性就能实现浮起来的效果。

然而还是有不同的形式的,及坑要踩。本文就简单介绍下

一. 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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  布局 scr 软键盘 android