当布局从纵向更改为横向时,我需要更改活动的背景颜色。
我已经添加了显示权限,并尝试使用setActivityBackgroundColor
中的onCreate
来更改背景颜色。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
int orientation = newConfig.orientation;
if (orientation == Configuration.ORIENTATION_PORTRAIT){
Log.d("tag", "Portrait");
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setActivityBackgroundColor(250);
}
else if (orientation == Configuration.ORIENTATION_LANDSCAPE){
Log.d("tag", "Landscape");
Toast.makeText(this, "Landscape", Toast.LENGTH_SHORT).show();
setActivityBackgroundColor(150);
}
else{
Log.w("tag", "other: " + orientation);
setActivityBackgroundColor(50);
}
}
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
答案 0 :(得分:0)
将此行添加到清单中的活动标签中。这将允许活动在方向更改时更改配置
android:configChanges="orientation|keyboardHidden|screenSize