La realización de la barra de estado inmersiva
1 Establezca la barra de estado transparente configurando el tema del tema
1) Configuración en el diseño xml: Nota: la barra de estado inmersiva no era compatible antes de 4.4, y la barra de estado después de 5.0 tendrá una sombra por defecto .
método de procesamiento: conjunto falso
y luego establecer el color de barra de estado es transparente
@android: color / transparente
2) establecida en el código
2. Asegúrese de que el contenido no se extiende a la zona de la barra de estado
1) a continuación, en la disposición android: fitsSystemWindows = "true"
2) Agregue una barra de estado de marcador de posición en el diseño
3) Establezca paddingTop directamente en el código y agregue una barra de estado de marcador de posición.
3. Problema del menú deslizante:
1) El menú 5.0 tiene una sombra: la solución es agregue app: insetForeground = "# 00000000" a NavigationView
2) 4.4 Puede establecer fitSystemWidows en el diseño más externo Es verdadero y establecer clipToPadding en falso
El código de representación
es el siguiente
public class SecondActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.0以上
// Window window = getWindow();
// WindowManager.LayoutParams attributes = window.getAttributes();
// attributes.flags |= flagTranslucentNavigation;
// window.setAttributes(attributes);
getWindow().setStatusBarColor(Color.TRANSPARENT);
Window window = getWindow();
View decorView = window.getDecorView();
//两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
} else {
Window window = getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.flags |= flagTranslucentStatus | flagTranslucentNavigation;
window.setAttributes(attributes);
}
}
//设置 paddingTop xml中不能再设置fitSystemWidows为true了
ViewGroup rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.setPadding(0, 0, 0, getNavigationBarHeight());
Toolbar toolbar = findViewById(R.id.toolbar);
//设置padding
//设置高度加上状态栏高度
setHeightAndPadding(toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {
drawer.setFitsSystemWindows(true);
drawer.setClipToPadding(false);
}
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
public void setHeightAndPadding(View view) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height += getStatusBarHeight();
view.setPadding(view.getPaddingLeft(), view.getPaddingTop() + getStatusBarHeight(), view.getPaddingRight(), view.getPaddingBottom());
}
/**
* 获取状态栏高度
*/
public int getStatusBarHeight() {
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
return getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
private int getNavigationBarHeight() {
int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return getResources().getDimensionPixelSize(resourceId);
}
return 0;
}
@Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_tools) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
Sobre