专栏名称: 程序员大咖
为程序员提供最优质的博文、最精彩的讨论、最实用的开发资源;提供最新最全的编程学习资料:PHP、Objective-C、Java、Swift、C/C++函数库、.NET Framework类库、J2SE API等等。并不定期奉送各种福利。
目录
相关文章推荐
51好读  ›  专栏  ›  程序员大咖

Android标题栏、状态栏图标文字颜色及背景动态变化

程序员大咖  · 公众号  · 程序员  · 2018-04-20 10:24

正文

请到「今天看啥」查看全文


void switchTo ( int position) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0 : //首页
hideShowFragment(transaction, fourFragment, thirdFragment, secondFragment, homeFragment); //展示第一个fragment
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().getDecorView().findViewById(android.R.id.content).setPadding( 0 , 0 , 0 , CommonUtils.navigationHeight);
break ;
case 1 : //活动
hideShowFragment(transaction, homeFragment, thirdFragment, fourFragment, secondFragment); //展示第二个fragment
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//实现状态栏图标和文字颜色为暗色
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
getWindow().getDecorView().findViewById(android.R.id.content).setPadding( 0 , 0 , 0 , CommonUtils.navigationHeight);
break ;
case 2 : //所有图片
hideShowFragment(transaction, homeFragment, fourFragment, secondFragment, thirdFragment);
//展示第三个fragment
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//实现状态栏图标和文字颜色为暗色
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
getWindow().getDecorView().findViewById(android.R.id.content).setPadding( 0 , 0 , 0 , CommonUtils.navigationHeight);
break ;
case 3 : //我的
hideShowFragment(transaction, homeFragment, secondFragment, thirdFragment, fourFragment); //展示第四个fragment
//实现状态栏图标和文字颜色为浅色
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
getWindow().getDecorView().findViewById(android.R.id.content).setPadding( 0 , 0 , 0 , CommonUtils.navigationHeight);
break ;
default :
break ;
}
}
//fragment切换实现
private void hideShowFragment (FragmentTransaction transaction, Fragment fragment1, Fragment fragment2, Fragment fragment3, Fragment fragment4) {
transaction.hide(fragment1);
transaction.hide(fragment2);
transaction.hide(fragment3);
transaction.show(fragment4);
transaction.commitAllowingStateLoss();
}

大家可能会注意到,我这里切换每个fragment时,有下面这样一行代码:

getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, CommonUtils.navigationHeight);

这行代码干什么用的,因为我们这里首页和我的页面,需要背景图片填充到状态栏,故不能使用android:fitsSystemWindows属性,故在实现上面效果时带有底部导航栏手机上就会存在一个大坑,解决办法见第3章节。同时不使用android:fitsSystemWindows属性,怎么让布局不遮挡状态栏文字,解决办法见第4章节。

  1. 带有底部导航栏手机底部导航按钮会和navigationbar重叠
    如下图所示:


    底部导航栏与底部按钮重叠

全屏时,由于视图布局会填充到状态栏和导航栏下方,如果不使用android:fitsSystemWindows=”true”属性,就会使底部导航栏和应用底部按钮重叠,导视按钮点击失效,这该怎么办?

经过网上搜索相关资料,其实实现方法和实现透明状态栏效果方法一致。

解决的方法:

  1. 先判断手机是否有物理按钮判断是否存在NavigationBar;

  2. 计算底部的NavigationBar高度;

  3. 最后设置视图边距。

3.1 通过反射判断手机是否有物理按钮NavigationBar

//判断是否存在NavigationBar
public static boolean checkDeviceHasNavigationBar(Context context) {
   boolean hasNavigationBar = false;
   Resources rs = context.getResources();






请到「今天看啥」查看全文