When creating an empty activity in android studio, the actionbar is still
used as the default status bar. Using a custom layout in the actionbar can more flexibly meet more scenarios, functions, layouts, and interface requirements.
However, when the actionbar uses a custom layout, there will be a blank The situation that is not covered, as shown in the figure below:
Solution:
Toolbar parent =(Toolbar) baseBinding.getRoot().getParent();//我使用了viewbinding
//Toolbar parent =(Toolbar) customActionBarView.getParent();
parent.setPadding(0,0,0,0);
parent.setContentInsetsAbsolute(0,0);
This code works regardless of whether toolbar is used in your custom layout
Actionbar uses the complete code of custom layout:
//若继承自AppCompatActivity,使用:
ActionBar actionBar =getSupportActionBar();
//若继承自Activity,使用:
//ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(baseBinding.getRoot()); //我使用了viewbinding
//actionBar.setCustomView(R.layout.titlebar_layout);
actionBar.setElevation(0);
actionBar.getCustomView().findViewById(R.id.titlebar_left).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
onBackPressed();
}
});
Toolbar parent =(Toolbar) baseBinding.getRoot().getParent();
//Toolbar parent =(Toolbar) customActionBarView.getParent();
parent.setPadding(0,0,0,0);
parent.setContentInsetsAbsolute(0,0);
The final effect is as follows: