Android Material Design- ActionBar和Title即使将windowActionBar设置为false和windowNoTitle设置为true,仍然可见。

3
我正在尝试 Material Design。我在样式中创建了一个主题MyMaterialTheme样式:
<resources>
    <!-- Theme for the material design of the app -->
    <style name="MyMaterialTheme" parent="MyMaterialTheme.Base"></style>

    <style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="colorPrimary">@color/colorPrimaryApp</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkApp</item>
        <item name="colorAccent">@color/colorAccentApp</item>

    </style>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

在我的 AndroidManifest.xml 中,我包含了这个主题(MyMaterialTheme)。我可以确认我的主题已经应用,因为它已经使用了正确的颜色。我将 windowActionBar 分配为 false,并将 windowNoTitle 分配为 true,但当我运行应用时仍然看到 ActionBar 和标题。有人能指出我哪里出错了吗?
<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/MyMaterialTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
           <!-- android:theme="@style/AppTheme.NoActionBar"> -->
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

这里是模拟器的屏幕截图:

这些代码在你的样式中是重复的,顺便说一下:<item name="windowNoTitle">true</item> <item name="windowActionBar">false</item> - ʍѳђઽ૯ท
@LinX64 感谢您的评论。您是指AppTheme.NoActionBar中的项目吗? - Ankur Bhatia
这就像是Android Studio中的默认示例,就是这个:<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar" 我认为你应该将它们删除。顺便说一下,我尝试过这个:android:windowNoTitle,但那没用。除非你要使用Java,比如:requestWindowFeature(Window.FEATURE_NO_TITLE); - ʍѳђઽ૯ท
你能展示一下你的布局XML代码吗?即使你的应用程序主题没有工具栏,Android Studio的默认布局通常也会在每个布局中手动添加一个。 - TheoKanning
@TheoKanning 非常感谢。你说得对。我刚看到我的activity_main.xml,它有一个工具栏,并且默认情况下,mainActivity.java已经设置了它。删除后,它就消失了。 - Ankur Bhatia
太好了!我把那个作为答案发布了。 - TheoKanning
2个回答

2

请检查您的activity_main.xml文件。Android Studio的默认布局通常会在每个布局中手动添加工具栏,即使您的应用程序主题没有工具栏。


非常感谢。你是正确的。它被包含在activity_main.xml中。 - Ankur Bhatia

0

你的材料风格所继承的基础风格应该是 noTitlebar,就像这段代码一样

parent="Theme.AppCompat.Light.NoActionBar"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接