广告位联系
返回顶部
分享到

android实现按钮浮动在键盘上方

Android 来源:互联网搜集 作者:秩名 发布时间:2020-03-23 19:15:50 人浏览
摘要

大家好,我是梦辛工作室的灵,最近在帮客户修改安卓程序时,有要求到一个按钮要浮动在键盘的上方,下面大概讲一下实现方法: 其实很简单,分三步走 第一步 获取当前屏幕的高度 ? 1 2 3 4 Display defaultDisplay = mcontext.getWindowManager().getDefaultDi

大家好,我是梦辛工作室的灵,最近在帮客户修改安卓程序时,有要求到一个按钮要浮动在键盘的上方,下面大概讲一下实现方法:

其实很简单,分三步走

第一步 获取当前屏幕的高度

?
1
2
3
4
Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
  Point point = new Point();
  defaultDisplay.getSize(point);
  height = point.y;

第二步 获取当前屏幕可见区域的高度,用于判断当前键盘是否隐藏或显示

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void setFloatView(View root,View floatview){
  this.root = root; //根节点
  listener = new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    Rect r = new Rect();
    mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
    int heightDifference = height - (r.bottom - r.top); // 实际高度减去可视图高度即是键盘高度
    boolean isKeyboardShowing = heightDifference > height / 3;
    if(isKeyboardShowing){
     //键盘显示
    }else{
     //键盘隐藏
    }
   }
  };
  root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
 }

第三步 当键盘隐藏时让按钮 动画移动至原有位置,当前键盘显示时让按钮动画移动至当前键盘的高度上方   

?
1
2
3
4
5
6
7
if(isKeyboardShowing){
    //键盘显示
    floatview.animate().translationY(-heightDifference).setDuration(0).start();
   }else{
    //键盘隐藏
    floatview.animate().translationY(0).start();
   }

然后我为了方便封装了一个工具类 FloatBtnUtil,很好用,下面是代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
 * 梦辛灵 实现按钮浮动工具
 */
public class FloatBtnUtil {
 
 private static int height = 0;
 private Activity mcontext;
 private ViewTreeObserver.OnGlobalLayoutListener listener;
 private View root;
 
 public FloatBtnUtil(Activity mcontext){
  this.mcontext = mcontext;
  if (height == 0){
   Display defaultDisplay = mcontext.getWindowManager().getDefaultDisplay();
   Point point = new Point();
   defaultDisplay.getSize(point);
   height = point.y;
  }
 }
 
 public void setFloatView(View root,View floatview){
  this.root = root; //视图根节点 floatview // 需要显示在键盘上的View组件
  listener = new ViewTreeObserver.OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
    Rect r = new Rect();
    mcontext.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
    int heightDifference = height - (r.bottom - r.top);
    boolean isKeyboardShowing = heightDifference > height / 3;
    if(isKeyboardShowing){
     floatview.animate().translationY(-heightDifference).setDuration(0).start();
    }else{
     floatview.animate().translationY(0).start();
    }
   }
  };
  root.getViewTreeObserver().addOnGlobalLayoutListener(listener);
 }
 
 public void clearFloatView(){
  if (listener != null && root != null)
  root.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
 }
 
}

下面是使用代码:

?
1
2
3
4
5
6
private void initFloatBtn() {
 FloatBtnUtil floatBtnUtil = new FloatBtnUtil(this);
 LinearLayout lin_bottom = (LinearLayout) this.findViewById(R.id.lin_bottom);
 LinearLayout lin_root = (LinearLayout)this.findViewById(R.id.lin_root);
 floatBtnUtil.setFloatView(lin_root,lin_bottom);
}


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://blog.csdn.net/weixin_41392105/article/details/105010624
相关文章
  • Kotlin的Collection与Sequence操作异同点介绍

    Kotlin的Collection与Sequence操作异同点介绍
    在Android开发中,集合是我们必备的容器,Kotlin的标准库中提供了很多处理集合的方法,而且还提供了两种基于容器的工作方式:Collection 和
  • 实现一个Kotlin函数类型方法

    实现一个Kotlin函数类型方法
    接口与函数类型 业务开发中,经常会有实现一个函数式接口(即接口只有一个方法需要实现)的场景,大家应该都会不假思索的写出如下代
  • Android10 App启动Activity源码分析
    ActivityThread的main方法 让我们把目光聚焦到ActivityThread的main方法上。 ActivityThread的源码路径为/frameworks/base/core/java/android/app/ActivityThread。 1 2
  • Android10客户端事务管理ClientLifecycleManager源码解析

    Android10客户端事务管理ClientLifecycleManager源码解析
    在Android 10 App启动分析之Activity启动篇(二)一文中,简单地介绍了Activity的生命周期管理器是如何调度Activity进入onCreate生命周期的流程。这
  • Kotlin对象的懒加载方式by lazy与lateinit异同介绍

    Kotlin对象的懒加载方式by lazy与lateinit异同介绍
    属性或对象的延时加载是我们相当常用的,一般我们都是使用 lateinit 和 by lazy 来实现。 他们两者都是延时初始化,那么在使用时那么他们两
  • Android类加载流程分析

    Android类加载流程分析
    本文分析的代码基于Android8.1.0源码。 流程分析 从loadClass开始,我们来看下Android中类加载的流程 /libcore/ojluni/src/main/java/java/lang/ClassLoader.ja
  • Android实现读写USB串口数据的代码

    Android实现读写USB串口数据的代码
    最近在研究USB方面的内容;先后做了关于Android读写HID、串口设备的DEMO。本文比较简单,主要介绍的是Android实现读取串口数据的功能 废话不
  • Epoxy - 在RecyclerView中构建复杂界面
    Diffing 对于复杂数据结构支持的多个视图类型展示在屏幕上, Epoxy此时是尤其有用的. 在这些场景中, 数据可能会被网络请求, 异步 Observable, 用
  • Android性能优化的详细介绍

    Android性能优化的详细介绍
    性能优化是一个app很重要的一部分,一个性能优良的app从被下载到启动到使用都能给用户到来很好的体验。自然我们做性能优化也是从被下
  • Android进阶宝典-插件化2(Hook启动插件中四大组件

    Android进阶宝典-插件化2(Hook启动插件中四大组件
    在上一节,我们主要介绍了如果通过反射来加载插件中的类,调用类中的方法;既然插件是一个apk,其实最重要的是启动插件中的Activity、
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计