android:fitsSystemWindows属性的用法
属性说明
fitsSystemWindows属性可以让view根据系统窗口来调整自己的布局;简单点说就是我们在设置应用布局时是否考虑系统窗口布局,这里系统窗口包括系统状态栏、导航栏、输入法等,包括一些手机系统带有的底部虚拟按键。
android:fitsSystemWindows=”true” (触发View的padding属性来给系统窗口留出空间)
这个属性可以给任何view设置,只要设置了这个属性此view的其他所有padding属性失效,同时该属性的生效条件是只有在设置了透明状态栏(StatusBar)或者导航栏(NavigationBar)此属性才会生效。
注意: fitsSystemWindows只作用在Android4.4及以上的系统,因为4.4以下的系统StatusBar没有透明状态。
应用场景
在不同Android版本下,App状态栏和不同版本中系统本身的状态栏的适配;
兼容带有底部虚拟按键的手机系统。
属性使用
1、默认效果
先贴一张未对系统状态栏和导航栏做透明设置时测试布局效果图:
2、系统窗口透明后效果
当设置了透明状态栏(StatusBar)和透明导航栏(NavigationBar)时效果图:
透明状态栏代码设置:
//布局设置
<item name="android:windowTranslucentStatus">true</item>
//或者代码设置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
透明导航栏代码设置:
//布局设置
<item name="android:windowTranslucentNavigation">true</item>
//或者代码设置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
如果有以上两种情况之一,我们的状态栏(StatusBar)或导航栏(NavigationBar)就会变成透明,并且布局会扩展到StatusBar或NavigationBar的位置。
注意:这里有个关于状态栏和导航栏透明样式的问题,这个是受Android版本决定,4.4以下无透明效果,4.4~5.0是全透明,5.0以上是半透明。我使用的是5.0以上的版本模拟器进行测试的,所以是半透明。
3、设置fitsSystemWindows属性后效果
现在就到了我们关键的fitsSystemWindows属性登场了,只要在根布局中加上android:fitsSystemWindows=”true”效果如下图:
设置了android:fitsSystemWindows=”true”属性后针对透明的状态栏会自动添加一个值等于状态栏高度的paddingTop;针对透明的系统导航栏会自动添加一个值等于导航栏高度的paddingBottom
贴上布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#66FF4081"
android:gravity="center"
android:text="App标题栏"
android:textSize="30sp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0CE3EE"
android:gravity="center"
android:text="App内容部分"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#4188F8"
android:gravity="center"
android:text="App导航栏"
android:textSize="30sp"/>
</LinearLayout>
追及
附加一个获取状态栏StatusBar的和一个获取导航栏NavigationBar高度的java代码:
//返回值就是状态栏的高度,得到的值单位px
public float getStatusBarHeight() {
float result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimension(resourceId);
}
return result;
}
//返回值就是导航栏的高度,得到的值单位px
public float getNavigationBarHeight() {
float result = 0;
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimension(resourceId);
}
return result;
}
android:fitsSystemWindows属性的用法的更多相关文章
- Android SystemProperties设置/取得系统属性的用法总结
通过调查得知,Android系统中取得/设置系统属性的用法参考以下3篇文章就足够了. 1.Android SystemProperties简介 介绍了设置属性需要的权限,已经设置权限的方法. Syst ...
- android.view.animation(1) - alpha、scale、translate、rotate、set的xml属性和用法(转)
一.ScaleAnimation ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, floa ...
- Android 组件属性
属性名称 描述 android:background 设置背景色/背景图片.可以通过以下两种方法设置背景为透明:”@android:color/transparent”和”@null”.注意TextV ...
- EditText的一些属性及用法
EditText的一些属性及用法设置当EditText获得焦点时把文本框的内容全选中android:selectAllOnFocus="true"设置某个EditText默认获得焦 ...
- 怎么通过activity里面的一个按钮跳转到另一个fragment(android FragmentTransaction.replace的用法介绍)
即:android FragmentTransaction.replace的用法介绍 Fragment的生命周期和它的宿主Activity密切相关,几乎和宿主Activity的生命周期一致,他们之间最 ...
- Android layout属性大全
第一类:属性值 true或者 false android:layout_centerHrizontal 水平居中 android:layout_centerVertical 垂直居中 ...
- Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法
一.概述 Android的animation由四种类型组成:alpha.scale.translate.rotate,对应android官方文档地址:<Animation Resources&g ...
- android application类的用法
android application类的用法 Application是android系统Framework提供的一个组件,它是单例模式(singleton),即每个应用只有一个实例,用来存储系统的一 ...
- [转]Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法
转载:http://blog.csdn.net/harvic880925/article/details/39996643 前言:这几天做客户回访,感触很大,用户只要是留反馈信息,总是一种恨铁不成钢的 ...
随机推荐
- C#.NET常见问题(FAQ)-如何让listView如何选中一行
把FullRowSelect设置为True 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com/acetaohai123 我的在线论坛: ht ...
- spring boot使用slf4j输出日志
spring boot使用slf4j输出日志 https://blog.csdn.net/qq442270636/article/details/79406346 Spring Boot SLF4J日 ...
- vue中的ajax - axios
vue中的ajax - axios axios - 简书 使用 axios 实现 ajax 方案 VUE 更好的 ajax 上传处理 axios.js vue.js 自2.0版本已经不对 vue-re ...
- SSH使用Slf4j
1. Slf4j的使用 在上一篇随笔:SSH使用Log4j的基础上配置. (1)导入两个文件:slf4j-api-1.5.8.jar和slf4j-log4j12-1.5.8.jar. (2)在需要日志 ...
- C# 只能输入字母或数字
c# 只能输入字母或者数字 或者退格符 方法一:KeyPress private void textBox2_KeyPress(object sender, KeyPressEventArgs e) ...
- InfluxDB和MySQL的读写对比测试
今天进行了InfluxDB和MySQL的对比测试,这里记录下结果,也方便我以后查阅. 操作系统: CentOS6.5_x64InfluxDB版本 : v1.1.0MySQL版本:v5.1.73CPU ...
- Java 线程池之FixedThreadPool(Java代码实战-003)
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util ...
- 查询后n条记录
查询后n条记录 SELECT * FROM tb_stu ORDER BY id ASC LIMIT n
- maven正确的集成命令-U-B
http://healthandbeauty.iteye.com/blog/1618501 在持续集成服务器上使用怎样的 mvn 命令集成项目,这个问题乍一看答案很显然,不就是 mvn clean i ...
- Windows2008|2003超出最大连接数
问题描述: 终端服务器超出最大允许连接数的解决方法 00.以管理员回话形式登录(本质踢掉他人) mstsc /v:IP /console mstsc /admin /v:ip mstsc /con ...