【Android实战】Android沉浸式状态栏实现(下)
之前的Android沉浸式状态栏实现并没有考虑软键盘的影响,接下来的内容将会针对这个问题给出解决方式,先看一下效果图
这个是一个留言板的效果图:
即弹出软键盘的时候并不会导致整个布局上移。
详细怎样实现?依照下面步骤进行设置:
1、布局文件里声明例如以下
<activity
android:name="com.storm.durian.activity.LeaveMessageDetailsActivity"
android:screenOrientation="portrait"
/>
2、这里重写了RelativeLayout。并重写了fitSystemWindows,这个是最最关键的。
/**
* 自适应布局
*/
public class FitsSystemWindowsLayout extends RelativeLayout {
private static final String TAG = FitsSystemWindowsLayout.class.getSimpleName();
private SoftKeyBoardStateListener softKeyBoardStateListener; public FitsSystemWindowsLayout(Context context) {
super(context);
} public FitsSystemWindowsLayout(Context context, AttributeSet attrs) {
super(context, attrs);
} public FitsSystemWindowsLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
} @Override
protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) { dispatchListenerLow(heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} /**
* 处理低版本号键盘弹出
*
* @param heightMeasureSpec 高度
*/
private void dispatchListenerLow(int heightMeasureSpec) {
if (softKeyBoardStateListener == null || Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
return;
}
int oldSpec = getMeasuredHeight(); if (oldSpec <= 0) {
return;
}
int newSpec = MeasureSpec.getSize(heightMeasureSpec);
int offset = oldSpec - newSpec;
if (offset > 100) {
LogHelper.i(TAG, "键盘打开");
softKeyBoardStateListener.onSoftKeyBoardStateChange(true);
} else if (offset < -100) {
LogHelper.i(TAG, "键盘关闭");
softKeyBoardStateListener.onSoftKeyBoardStateChange(false);
}
} @Override
protected boolean fitSystemWindows(Rect insets) {
dispatchListener(insets);
insets.top = 0;
return super.fitSystemWindows(insets);
} /**
* 分发监听
*
* @param insets
*/
private void dispatchListener(Rect insets) {
if (softKeyBoardStateListener == null || android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
if (insets.top != 0 && insets.bottom != 0) {
LogHelper.i(TAG, "键盘打开");
softKeyBoardStateListener.onSoftKeyBoardStateChange(true);
} else {
LogHelper.i(TAG, "键盘关闭");
softKeyBoardStateListener.onSoftKeyBoardStateChange(false);
} } /**
* 设置软键盘监听事件
*
* @param softKeyBoardStateListener
*/
public void setSoftKeyBoardListener(SoftKeyBoardStateListener softKeyBoardStateListener) {
this.softKeyBoardStateListener = softKeyBoardStateListener;
} public interface SoftKeyBoardStateListener { public void onSoftKeyBoardStateChange(boolean isOpen); }
}
尽管以上布局也提供了键盘打开或者关闭的回调,可是在某些低版本号手机上还是支持的不太好,须要这个回调的能够将此回调作为当中一个方法。可是不要过分依赖
3、布局文件
使用com.storm.durian.view.FitsSystemWindowsLayout包裹整个布局,并设置android:fitsSystemWindows=“true”
<?xml version="1.0" encoding="utf-8"? >
<com.storm.durian.view.FitsSystemWindowsLayout android:id="@+id/leave_message_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/ececec"
android:fitsSystemWindows="true"> <include
android:id="@+id/leave_message_title"
layout="@layout/common_back"/> <LinearLayout
android:id="@+id/ll_leave_message_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@drawable/leave_message_bottom_bg"
android:orientation="horizontal"> <EditText
android:id="@+id/et_leave_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@drawable/et_leave_message_bg"
android:hint="亲,给我留言我会回复你哒~"
android:maxLines="4"
android:textColor="@color/_3e363d"
android:textSize="@dimen/text_size_small"/> <Button
android:id="@+id/btn_leave_message_send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="@drawable/btn_leave_message_send_selector"
android:singleLine="true"
android:text="发送"
android:textColor="@color/_3e363d"
android:textSize="@dimen/text_size_middle"/>
</LinearLayout> <ListView
android:id="@+id/lv_leave_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@id/ll_leave_message_input"
android:layout_below="@id/leave_message_title"
android:cacheColorHint="#00000000"
android:divider="@color/dbdbdb"
android:dividerHeight="1dp"
android:fadingEdge="none"/> <ViewStub
android:id="@+id/activity_leave_message_loading_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/leave_message_title"
android:inflatedId="@+id/activity_leave_message_loading_subTree"
android:layout="@layout/common_loading"/> <ViewStub
android:id="@+id/leave_message_empty_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ll_leave_message_input"
android:layout_below="@id/leave_message_title"
android:inflatedId="@+id/leave_message_empty_subTree"
android:layout="@layout/common_tips_layout"/> <ViewStub
android:id="@+id/leave_message_net_error_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/leave_message_title"
android:inflatedId="@+id/leave_message_net_error_subTree"
android:layout="@layout/common_net_error_layout"/> </com.storm.durian.view.FitsSystemWindowsLayout>
4、Activity中处理
设置setImmerseLayout(findViewById(R.id.leave_message_title));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave_detail_message);
setImmerseLayout(findViewById(R.id.leave_message_title));
logic = LeaveMessageLogic.getInstance(getApplicationContext());
mHandler = new MyHandler(this);
initView();
}
再看BaseActivity中的setImmerseLayout方法
版本号要求仍然是4.4以上
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isStart = true;
ShareSDK.initSDK(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//透明状态栏
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//透明导航栏
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
statusBarHeight = ScreenUtil.getStatusBarHeight(this);
}
ScreenManager.getScreenManager().pushActivity(this);
}
protected void setImmerseLayout(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight = ScreenUtil.getStatusBarHeight(this.getBaseContext());
view.setPadding(0, statusBarHeight, 0, 0);
}
}
OK。须要的朋友们能够自己尝试一下!
有空的话会将这个沉浸式的留言板的功能写一下分享出来!
【Android实战】Android沉浸式状态栏实现(下)的更多相关文章
- Android中的沉浸式状态栏效果
无意间了解到沉浸式状态栏,感觉贼拉的高大上,于是就是试着去了解一下,就有了这篇文章.下面就来了解一下啥叫沉浸式状态栏.传统的手机状态栏是呈现出黑色条状的,有的和手机主界面有很明显的区别.这一样就在一定 ...
- Android 4.4沉浸式状态栏的实现
要实现Android 4.4上面的沉浸式状态栏要用到开源项目SystemBarTint(https://github.com/hexiaochun/SystemBarTint) public clas ...
- Android Studio 关于沉浸式状态栏
values-v19/style.xml <style name="AppTheme" parent="Theme.AppCompat.Light.NoAction ...
- [置顶]
Xamarin android沉浸式状态栏
虽然关于android "沉浸式"状态栏有很多博客介绍过,从小菜到大神无一例外.我第一次看到这种"沉浸"式的效果我也以为真的是这么叫,然而根本不是这么回事,完全 ...
- 81.Android之沉浸式状态栏攻略
转载:http://blog.csdn.net/lmj623565791/article/details/48649563/ 一.概述 近期注意到QQ新版使用了沉浸式状态栏,ok,先声明一下:本篇博客 ...
- android沉浸式状态栏设置(4.4以上版本)
其实设置比较简单,我用了小米和htc的几款机型都可以用. 主要代码就是这个(注意要在Activity的setContentView之前调用才行) /** * 开启沉浸式状态栏 * */ public ...
- Android 沉浸式状态栏攻略 让你的状态栏变色吧
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48649563: 本文出自:[张鸿洋的博客] 一.概述 近期注意到QQ新版使用了 ...
- Android 沉浸式状态栏完美解决方案
现在搜索Android 沉浸式状态栏,真的是一堆一堆,写的特别多,但是真正用的舒服的真没有,在这里自己整理一下开发记录 注意,在使用这个步骤过程之前,请把之前设置的代码注释一下 把布局带有androi ...
- android -------- 沉浸式状态栏和沉浸式导航栏(ImmersionBar)
android 4.4以上沉浸式状态栏和沉浸式导航栏管理,包括状态栏字体颜色,适用于Activity.Fragment.DialogFragment.Dialog,并且适配刘海屏,适配软键盘弹出等问题 ...
随机推荐
- 安卓4.3以上版本已经完美支持BLE(英文版)
Android 4.3 (API Level 18) introduces built-in platform support for Bluetooth Low Energy in the cent ...
- 查看spark是否有僵尸进程,有的话,先杀掉。可以使用下面命令
查看spark是否有僵尸进程,有的话,先杀掉.可以使用下面命令yarn application -listyarn application -kill <jobid>
- nyoj--108--士兵杀敌(一)(区间求和&&树状数组)
士兵杀敌(一) 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的. 小工是南将军手下的军师,南将军现在想知 ...
- PostgreSQL Replication之第七章 理解Linux高可用(4)
7.4 术语与概念 一组计算机被称为集群.集群内的一台计算机被称为一个节点. 当集群内的节点数量是 N (2,,3,等.) ,那么我们讨论一个N节点的集群. 高可用性软件,传输层和集群管理层都运行于每 ...
- hdu 3292 No more tricks, Mr Nanguo
No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Jav ...
- NodeJS学习笔记 (12)网络地址解析-url(ok)
模块概述 nodejs中,提供了url这个非常实用的模块,用来做URL的解析.在做node服务端的开发时会经常用到.使用很简单,总共只有3个方法. 正式讲解前,各位同学先把下面这个图记在心上(来自no ...
- spring-data-jpa 新增 修改 删除 查询 分页
1.查询所有数据 findAll() 2.分页查询 findAll(new PageRequest(0, 2)) 3.根据id查询 findOne() 4.根据实体类属性查询: findByPro ...
- HDU 4862 Jump 费用流
又是一个看了题解以后还坑了一天的题…… 结果最后发现是抄代码的时候少写了一个负号. 题意: 有一个n*m的网格,其中每个格子上都有0~9的数字.现在你可以玩K次游戏. 一次游戏是这样定义的: 你可以选 ...
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
[题目链接]:http://codeforces.com/contest/821/problem/B [题意] 当(x,y)这个坐标中,x和y都为整数的时候; 这个坐标上会有x+y根香蕉; 然后给你一 ...
- MyBatis学习总结(17)——Mybatis分页插件PageHelper
如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...