我有一个 VideoView 并且正在流式传输 mp4 文件,它工作正常。但是,我试图在纵向时将视频设置为屏幕居中,而在横向时将其设置为全屏。我正在尝试的代码如下,但它不起作用。如果我切换到横向,它不会改变任何东西,直到我切换回纵向,然后它是全屏的,对于手机的纵向视图来说太大了。如何使我的视频视图在横向时调整为全屏,并在纵向时切换回标准居中在父级中?我已将 configChanges="orientation" 添加到清单中的活动中。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
videoView.setLayoutParams(lp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) videoView.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
videoView.setLayoutParams(lp);
}
}
另外,我的印象是在布局参数之后设置 0,1(false,true)将添加规则或删除它。这个对吗?
预先感谢您的任何帮助!