新建项目运行效果图如下:
如果你以为上图是上图那样的,但是它确实下面这样的,你只需要修改TextView组件填充全屏(设置TextView的宽高为"match_parent")和背景色为灰色,比如“#DBDBD”,就能发现:
下main让我们一步一步的去掉这些东西,方便我们进行沉浸式状态栏开发准备:
先来去掉顶部app的标题栏:
package com.xw.bottomnavigationapp;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.xw.bottomnavigationapp.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
//下面那行代码注掉后,这个变量也没用了,也可以注销掉
// AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
// R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
// .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
//注掉下面这行代码
//NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(binding.navView, navController);
}
}
将manifest文件中的theme主题也改掉:
修改主题为:Theme.AppCompat.Light.NoActionBar
再来去掉顶部这块空白区域:
你到MainActivity对应的布局文件中删掉创建项目时自动加上的paddingtop的高度即可:
效果如下:
现在你可以舒舒服服的编写沉浸式状态栏的代码了。
,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)