在onConfigurationChanged上你可以尝试运行
handler.postDelayed(new Runnable(){
@Overrride
public void run(){
toolbar.invalidate();
}
},[Try different time lapses(miliseconds)]);
您可以在配置更改(方向)上检查AppBarLayout的状态,并将其存储。然后,您可以在应用配置更改后为AppBarLayout设置该状态。
公共类CheeseDetailActivity扩展AppCompatActivity {
public static final String EXTRA_NAME =" cheese_name&#34 ;;
AppBarLayout appBarLayout;
boolean isCollapsed = false;
//状态改变监听器
AppBarLayout.OnOffsetChangedListener toolbarStateListener = new AppBarLayout.OnOffsetChangedListener(){
@覆盖
public void onOffsetChanged(AppBarLayout appBarLayout,int verticalOffset){
if(verticalOffset == 0){
//倒塌
isCollapsed = true;
} else {
//没有崩溃
isCollapsed = false;
}
}
};
@覆盖
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_detail);
Intent intent = getIntent();
final String cheeseName = intent.getStringExtra(EXTRA_NAME);
最终工具栏工具栏=(工具栏)findViewById(R.id.toolbar);
setSupportActionBar(栏);
getSupportActionBar()setDisplayHomeAsUpEnabled(真)。
CollapsingToolbarLayout collapsingToolbar =
(CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(cheeseName);
//获取应用栏布局
appBarLayout =(AppBarLayout)findViewById(R.id.appbar);
//设置监听器以监听应用栏布局的状态变化
appBarLayout.addOnOffsetChangedListener(toolbarStateListener);
loadBackdrop();
}
@覆盖
public void onSaveInstanceState(Bundle savedInstanceState){
super.onSaveInstanceState(savedInstanceState);
//保存方向变化的状态
savedInstanceState.putBoolean(" isCollapsed",isCollapsed);
}
@覆盖
public void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
//获取方向变化的状态
isCollapsed = savedInstanceState.getBoolean(" isCollapsed");
}
@覆盖
protected void onResume(){
super.onResume();
//设置应用栏布局的状态
appBarLayout.setExpanded(isCollapsed);
}
private void loadBackdrop(){
最终ImageView imageView =(ImageView)findViewById(R.id.backdrop);
。Glide.with(本).load(Cheeses.getRandomCheeseDrawable())centerCrop()代入(ImageView的)。
}
@覆盖
public boolean onCreateOptionsMenu(Menu menu){
getMenuInflater()。inflate(R.menu.sample_actions,menu);
返回true;
}
}
我在CheeseSquare存储库中编辑了CheeseDetailActivity。