正文
void
switchTo
(
int
position)
{
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
switch
(position) {
case
0
:
hideShowFragment(transaction, fourFragment, thirdFragment, secondFragment, homeFragment);
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);
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);
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);
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
;
}
}
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章节。
-
带有底部导航栏手机底部导航按钮会和navigationbar重叠
如下图所示:
底部导航栏与底部按钮重叠
全屏时,由于视图布局会填充到状态栏和导航栏下方,如果不使用android:fitsSystemWindows=”true”属性,就会使底部导航栏和应用底部按钮重叠,导视按钮点击失效,这该怎么办?
经过网上搜索相关资料,其实实现方法和实现透明状态栏效果方法一致。
解决的方法:
-
先判断手机是否有物理按钮判断是否存在NavigationBar;
-
计算底部的NavigationBar高度;
-
最后设置视图边距。
3.1 通过反射判断手机是否有物理按钮NavigationBar
public static boolean checkDeviceHasNavigationBar(Context context) {
boolean hasNavigationBar = false;
Resources rs = context.getResources();