一,xml 布局
二,MainActivity 方法
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//判断版本,如果[4.4,5.0)就设置状态栏和导航栏为透明
if (isFitVerson()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//设置虚拟导航栏为透明
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
setContentView(R.layout.activity_main3);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
//解决低版本4.4+的虚拟导航栏的
View nav = findViewById(R.id.navagation);
setSupportActionBar(mToolbar);
setOrChangeTranslucentColor(mToolbar, nav, Color.BLUE);
}
@SuppressLint("NewApi")
public void setOrChangeTranslucentColor(Toolbar toolbar, View bottomNavigationBar, int translucentPrimaryColor) {
//判断版本,如果[4.4,5.0)就设置状态栏和导航栏为透明
if (isFitVerson()) {
if (toolbar != null) {
//1.先设置toolbar的高度
ViewGroup.LayoutParams params = toolbar.getLayoutParams();
int statusBarHeight = getStatusBarHeight();
params.height += statusBarHeight;
toolbar.setLayoutParams(params);
//2.设置paddingTop,以达到状态栏不遮挡toolbar的内容。
toolbar.setPadding(
toolbar.getPaddingLeft(),
toolbar.getPaddingTop() + getStatusBarHeight(),
toolbar.getPaddingRight(),
toolbar.getPaddingBottom());
//设置顶部的颜色
toolbar.setBackgroundColor(translucentPrimaryColor);
}
if (bottomNavigationBar != null) {
//解决低版本4.4+的虚拟导航栏的
if (hasNavigationBarShow(getWindowManager())) {
ViewGroup.LayoutParams p = bottomNavigationBar.getLayoutParams();
p.height += getNavigationBarHeight();
bottomNavigationBar.setLayoutParams(p);
//设置底部导航栏的颜色
bottomNavigationBar.setBackgroundColor(translucentPrimaryColor);
}
}
} else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
getWindow().setNavigationBarColor(translucentPrimaryColor);
getWindow().setStatusBarColor(translucentPrimaryColor);
} else {
//<4.4的,不做处理
}
}
//判断是否是合适的低版本
private boolean isFitVerson() {
return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
&& android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP;
}
/**
* 判断是否有导航栏
* @param wm
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean hasNavigationBarShow(WindowManager wm){
Display display = wm.getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
//获取整个屏幕的高度
display.getRealMetrics(outMetrics);
int heightPixels = outMetrics.heightPixels;
int widthPixels = outMetrics.widthPixels;
//获取内容展示部分的高度
outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
int heightPixels2 = outMetrics.heightPixels;
int widthPixels2 = outMetrics.widthPixels;
LogUtils.e(heightPixels);
LogUtils.e(heightPixels2);
int w = widthPixels-widthPixels2;
int h = heightPixels-heightPixels2;
LogUtils.e(h);
return w>0||h>0;//竖屏和横屏两种情况。
}
/**
* 获取状态栏的高度
*
* @return
*/
private int getStatusBarHeight() {
return getSystemComponentDimen("status_bar_height");
}
//虚拟导航栏的
private int getNavigationBarHeight() {
return getSystemComponentDimen("navigation_bar_height");
}
public int getSystemComponentDimen(String dimenName) {
/**
* 获取状态栏高度——方法1
* */
int statusBarHeight1 = -1;
//获取status_bar_height资源的ID (String name, String defType, String defPackage)
int resourceId = getResources().getIdentifier(dimenName, "dimen", "android");
if (resourceId > 0) {
//根据资源ID获取响应的尺寸值
statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight1;
}
}