这是为您解决的问题。您必须通过代码动态更新控件边距。想法是在布局文件中添加一个带有控件的叠加层。并且当导航栏可见时,更新您的控件底部边距以将它们向上推到屏幕上。休息代码解释它对我有用。
布局文件:video_player_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView"/>
<TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="Video Controls" android:textStyle="bold" android:textSize="40sp"
android:textColor="#FFF"
android:background="#444444"
android:gravity="center"
android:layout_alignParentBottom="true"/>
<View android:visibility="gone" android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/overlay"/>
</RelativeLayout>
视频播放器.java
package com.vpiitsolution.stack;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import static android.view.View.SYSTEM_UI_FLAG_FULLSCREEN;
public class VideoPlayer extends AppCompatActivity {
VideoView videoView;
MediaController mediaController;
private String TAG = VideoPlayer.class.getSimpleName();
private View overlay;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_activity);
videoView = findViewById(R.id.videoView);
mediaController = new MediaController(this);
//videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.video1);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.start();
hideSystemUI();
overlay = findViewById(R.id.overlay);
overlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hideSystemUI();
}
});
videoView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
if ((visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0) {
updateControls(getNavigationBarHeight());
overlay.setVisibility(View.VISIBLE);
} else {
updateControls(0);
overlay.setVisibility(View.GONE);
}
}
});
}
int getNavigationBarHeight() {
Resources resources = getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
if (resourceId > 0) {
return resources.getDimensionPixelSize(resourceId);
}
return 0;
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
hideSystemUI();
} else {
showSystemUI();
}
}
private void hideSystemUI() {
View decorView = getWindow().getDecorView();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
void updateControls(int bottomMargin) {
TextView tv = findViewById(R.id.text);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) tv.getLayoutParams();
params.bottomMargin = bottomMargin;
tv.setLayoutParams(params);
}
/* private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}*/
private void showSystemUI() {
Log.e(TAG, "111");
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
}