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

Android应用横竖屏切换时起始侧出现边距问题求助

问题描述

此前多个commit版本中,应用横竖屏均为全屏正常显示;但目前切换至横屏模式时,屏幕顶部或底部出现边距,且该边距区域背景色未在主题文件中定义。回退至原本正常的commit版本后,问题仍存在。最后一次操作为生成Release APK及配置ProGuard规则,怀疑可能是设备问题。

相关代码

MainActivity布局

<?xml version="1.0" encoding="utf-8"?>
<layout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/my_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".ui.PopcornApplication"
        android:supportsRtl="true"
        android:theme="@style/Theme.Popcorn"
        tools:targetApi="31">
        <activity
            android:name=".ui.MainActivity"
            android:theme="@style/Theme.Popcorn"
            android:exported="true"
            android:launchMode="singleTop"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />
    </application>

</manifest>

THEME

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.Popcorn" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary"> @color/royalBlue</item>
        <item name="colorPrimaryVariant">@color/teal_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <item name="android:windowBackground">@drawable/splash_bg</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/royalBlue</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/white</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" >@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <!-- Customize your theme here. -->
    </style>


    <style name="MyDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:background">#393636</item>
    </style>
    
    <style name="ratingBarStyle" parent="Widget.AppCompat.RatingBar.Small">
        <item name="android:progressTint">@color/white</item>
        <item name="android:progressBackgroundTint">@color/white</item>
    </style>

</resources>

排查与解决建议

  • 检查设备显示设置:部分安卓设备的「显示缩放」「屏幕顶部显示」等系统设置会强制给应用添加边距,尝试重置为默认设置后测试
  • 区分Debug/Release版本验证:如果Debug版本正常,仅Release版出现问题,大概率是ProGuard混淆导致窗口相关类被破坏,可添加以下规则到ProGuard配置文件:
    -keep class android.view.WindowManager** { *; }
    -keep class android.view.Window** { *; }
    -keep class android.view.ViewGroup** { *; }
    
  • 补充全屏相关配置:
    1. 在Manifest的MainActivity节点中添加属性:
      android:resizeableActivity="true"
      android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout"
      
    2. 在Theme.Popcorn中添加全屏相关item:
      <item name="android:windowFullscreen">true</item>
      <item name="android:windowContentOverlay">@null</item>
      <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item> <!-- 适配刘海屏 -->
      
  • 检查splash_bg drawable:边距背景可能来自该全局背景图,验证其横屏适配是否正常,可临时替换为纯色背景测试
  • 清除应用缓存:卸载应用后清除残留缓存,再重新安装测试,排除系统缓存干扰

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 10:32:02