You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio实现VideoView横竖屏全屏自适应调整求助

Fixing VideoView Fullscreen Adaptation for Landscape/Portrait in Android Studio

Hey there! I see you're new to Android Studio and working on a video player app, struggling with getting VideoView to adapt properly to both landscape and portrait orientations while staying fullscreen. Let's break down the issues with your current code and fix this step by step.

First, Let's Address the Problem in Your Current Code

Right now, your onCreate method is forcing an orientation switch as soon as the activity starts—this creates a loop (switching from portrait to landscape, then back again immediately) and doesn't actually let the VideoView adapt naturally to each orientation. That's why you're seeing those toasts pop up repeatedly instead of getting the fullscreen behavior you want.

Step 1: Clean Up the Orientation Forcing Code

First, remove this entire block from your onCreate method—it's working against what you want to achieve:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
    Toast.makeText(getBaseContext(),"wa9fa",Toast.LENGTH_SHORT).show();
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
} else if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
    Toast.makeText(getBaseContext(),"na3sa",Toast.LENGTH_SHORT).show();
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

Step 2: Configure Your VideoView Layout for Fullscreen

Open your activity_main.xml layout file, and make sure your VideoView is set to fill the screen while maintaining the video's aspect ratio. Here's a recommended layout using ConstraintLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • android:adjustViewBounds="true" ensures the video keeps its original aspect ratio instead of stretching to fit the screen.
  • Setting width/height to match_parent with constraints to all parent edges makes it fill the screen in both orientations.

Step 3: Handle Screen Rotation Without Restarting the Video

By default, when the screen rotates, Android restarts your activity, which will pause and restart the video. To prevent this, add android:configChanges="orientation|screenSize" to your activity in the AndroidManifest.xml:

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

This tells Android to not restart the activity when orientation or screen size changes, so your video keeps playing seamlessly.

Step 4: Optional: Hide System Bars for True Fullscreen

If you want to hide the status bar and navigation bar for a more immersive experience, add this method to your Activity and call it when the video starts or when orientation changes:

private void enterFullscreen() {
    // Hide status bar and navigation bar
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    decorView.setSystemUiVisibility(uiOptions);
}

Modified Activity Code

Here's your updated MainActivity code with all the fixes:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    VideoView videoView = findViewById(R.id.videoView);
    String path = "android.resource://" + getPackageName() + "/" + R.raw.track01;
    videoView.setVideoURI(Uri.parse(path));

    MediaController mediaController = new MediaController(this);
    mediaController.setMediaPlayer(videoView);
    mediaController.setAnchorView(videoView);
    videoView.setMediaController(mediaController);

    // Enter fullscreen when video is ready to play
    videoView.setOnPreparedListener(mp -> {
        enterFullscreen();
        videoView.start();
    });
}

private void enterFullscreen() {
    View decorView = getWindow().getDecorView();
    int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
    decorView.setSystemUiVisibility(uiOptions);
}

// Re-apply fullscreen after orientation change
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    enterFullscreen();
}

How This Works

  • The VideoView now fills the screen in both portrait and landscape while preserving the video's aspect ratio.
  • Screen rotation no longer restarts the activity, so playback continues without interruption.
  • The optional fullscreen method hides system bars for a more immersive viewing experience.

Give this a try, and let me know if you run into any other issues! 😊

内容的提问来源于stack exchange,提问作者mani hani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:12:47