问题
I've created an Android app in Android Studio, and it has created these styles by default:
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
However, in an activity, I try to set this as a theme:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:theme="@style/AppTheme.NoActionBar">
But when I run the app I'm getting the action bar:
Why am I getting an action bar? Am I doing something obviously wrong, or is Android Studio, Google's official IDE for it's own platform, trying to confuse/mislead its developers?
Here is my activity code, there's nothing in it that can cause the action bar to appear by itself:
public class WelcomeActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_welcome);
}
}
回答1:
Try something like this:
<style name="AppTheme.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
and set this as the theme of your activity in the manifest, that is:
<activity
...
android:theme="@style/AppTheme.NoActionBar">
</activity>
回答2:
None of the other answers worked for me, but I found this solution:
Open values/styles.xml
in Android Studio and change this:
<!-- 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>
to:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- changed .DarkActionBar to .NoActionBar -->
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
回答3:
If you just let Android Studio auto generate the activity, look in activity_welcome
. There is be android.support.design.widget.AppBarLayout
and if you delete it, the ActionBar should be gone.
回答4:
You might have an action bar in your layout, or set theme in AndroidManifest file
回答5:
Once WelcomeActivity extends AppCompatActivity, try Theme.AppCompat.NoActionBar:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>
If not already done, include a version of appcompat library to your build.gradle:
dependencies {
compile 'com.android.support:appcompat-v7:24.1.1'
}
回答6:
You should add below
<item name="android:windowFullscreen">true</item>
<item name="windowActionBar">false</item>
You can use
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
You can visit Full Screen Theme for AppCompat
回答7:
you should use this theme in manifest.xml, same as:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
And in styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
回答8:
Try checking the AndroidManifest.xml file. I found that the main theme was established for the app, but the theme I wanted to apply had to be added into the correct activity.
回答9:
I had a similar problem with one of my activities which was not main. I had something like in the Mehmet K's answer as theme:
<style name="MyActivitysTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
I just added the parent theme too:
<style name="MyActivitysTheme" parent="AppTheme">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
I guess this way it successfully override the main theme
来源:https://stackoverflow.com/questions/36478925/why-is-apptheme-noactionbar-not-working