Android继承AppCompatActivity中一开始显示标题栏,后来不显示标题栏了,设置好菜单也无法显示了。

在清单文件AndroidManifest.xml中如下两句:

android:theme="@style/Theme.xxxx"
tools:replace="android:theme"

打开\res\values\themes.xml文件:

<!-- Base application theme. -->
    <style name="Base.Theme.xxxx" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

将其中的"Theme.Material3.DayNight.NoActionBar"改为

Theme.MaterialComponents.DayNight.DarkActionBar

问题解决,标题栏和下拉菜单显示正常。

特别注意,要在<style name="Base.Theme.xxxx"中修改,而不是<style name="Theme.xxxx"中修改。

另一种办法,直接在清单文件AndroidManifest.xml中设置主题:

android:theme="@style/Theme.AppCompat.DayNight.DarkActionBar"

猜你喜欢

转载自blog.csdn.net/anfenggang/article/details/131367450