求助:如何隐藏ActionBar并实现VideoView全屏?横竖屏切换适配问题
Hey there! Let's work through those two annoying issues you're having with your VideoView: hiding the ActionBar and making the video fill the screen completely, even when rotating the device.
1. First: Fix ActionBar Hiding & Initial Fullscreen Setup
Looking at your code snippet, there's a conflicting line that's probably keeping the ActionBar visible: you're requesting Window.FEATURE_NO_TITLE but then asking for Window.FEATURE_ACTION_BAR right after. That's why the ActionBar won't hide!
Here's the corrected onCreate method, plus the flags needed for true fullscreen:
public class RowClick extends AppCompatActivity { VideoView videoView; MediaController mediaController; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Request no title bar first requestWindowFeature(Window.FEATURE_NO_TITLE); // Enable fullscreen mode getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.your_activity_layout); // Replace with your actual layout file videoView = findViewById(R.id.videoView); mediaController = new MediaController(this); mediaController.setAnchorView(videoView); // Rest of your video initialization code (set video URI, etc.) } }
Don't Forget Your Layout!
Make sure your VideoView is set to fill the entire screen in your XML layout:
<VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" />
2. Fixing Fullscreen on Rotation
When you rotate the device, the system might reset your window settings or even recreate the Activity. Here's how to stop that and keep the video fullscreen:
Step 1: Update AndroidManifest.xml
Add configChanges to your Activity declaration so the system doesn't recreate it on rotation, and set a fullscreen theme (optional but cleaner):
<activity android:name=".RowClick" android:configChanges="orientation|screenSize|smallestScreenSize" android:theme="@style/FullScreenVideoTheme" android:screenOrientation="sensor" />
Step 2: Create a Fullscreen Theme (Optional but Recommended)
Add this to your res/values/styles.xml to handle fullscreen via theme instead of code:
<style name="FullScreenVideoTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowFullscreen">true</item> </style>
Step 3: Handle Rotation in Code
Override onConfigurationChanged to reapply fullscreen settings and adjust the VideoView layout when the screen rotates:
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // Re-enable fullscreen getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // Double-check ActionBar is hidden (just in case) if (getSupportActionBar() != null) { getSupportActionBar().hide(); } // Force VideoView to fill the new screen size ViewGroup.LayoutParams params = videoView.getLayoutParams(); params.width = ViewGroup.LayoutParams.MATCH_PARENT; params.height = ViewGroup.LayoutParams.MATCH_PARENT; videoView.setLayoutParams(params); // Reset MediaController anchor to the VideoView if (mediaController != null) { mediaController.setAnchorView(videoView); } }
Quick Notes
- If you're using a newer Android version (API 30+), you might need to handle edge-to-edge display, but the above steps should work for most cases.
- Test on a physical device if possible—emulators sometimes have weird fullscreen behavior.
内容的提问来源于stack exchange,提问作者basaya

