How to hide the title bar for an Activity in XML with existing custom theme

前端 未结 30 1630
误落风尘
误落风尘 2020-11-22 03:28

I want to hide the titlebar for some of my activities. The problem is that I applied a style to all my activities, therefore I can\'t simply set the theme to @android:

相关标签:
30条回答
  • 2020-11-22 03:50

    I found two reasons why this error might occur.

    One. The Window flags are set already set inside super.onCreate(savedInstanceState); in which case you may want to use the following order of commands:

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      
    
    super.onCreate(savedInstanceState);
    

    Two. You have the Back/Up button inside the TitleBar, meaning that the current activity is a hierarchical child of another activity, in which case you might want to comment out or remove this line of code from inside the onCreate method.

    getActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 2020-11-22 03:51

    You can modify your AndroidManifest.xml:

    <activity android:name=".MainActivity"
              android:label="@string/app_name"
              android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    

    or use android:theme="@android:style/Theme.Black.NoTitleBar" if you don't need a fullscreen Activity.

    Note: If you've used a 'default' view before, you probably should also change the parent class from AppCompatActivity to Activity.

    0 讨论(0)
  • 2020-11-22 03:51

    I don't like the this.requestWindowFeature(Window.FEATURE_NO_TITLE); because the title bar appears briefly, then disappears.

    I also don't like the android:theme="@android:style/Theme.NoTitleBar" because I lost all of the 3.0+ Holo changes that the users of the new devices have gotten used to. So I came across this solution.

    In your res/values folder make a file called styles.xml (If it doesn't already exist). In that file place the following code:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="Theme.Default" parent="@android:style/Theme"></style>
        <style name="Theme.NoTitle" parent="@android:style/Theme.NoTitleBar"></style>
        <style name="Theme.FullScreen" parent="@android:style/Theme.NoTitleBar.Fullscreen"></style>
    </resources>
    

    Next create a res/values-v11 with another styles.xml file (Once again this may already exist). In that file place the following code:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="Theme.Default" parent="@android:style/Theme.Holo"></style>
        <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.NoActionBar"></style>
        <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.NoActionBar.Fullscreen"></style>
    </resources>
    

    And if you are targeting 4.0+, create a res/values-v14 folder with yet another styles.xml file (Yes it may already be there). In that file place the following code:

    <resources xmlns:android="http://schemas.android.com/apk/res/android">
        <style name="Theme.Default" parent="@android:style/Theme.Holo.Light"></style>
        <style name="Theme.NoTitle" parent="@android:style/Theme.Holo.Light.NoActionBar"></style>
        <style name="Theme.FullScreen" parent="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen"></style>
    </resources>
    

    Finally, with all of these files created, open your AndroidManifiest.xml file you can add the code:

    android:theme="@style/Theme.NoTitle"
    

    to the activity tag of the activity you want no title for or the application tag if you want it to apply to the entire application.

    Now your users will get the themes associated with their device version with the screen layout you desire.

    P.S. Changing the value to android:theme="@style/Theme.FullScreen" will have the same effect, but also remove Notification bar.

    0 讨论(0)
  • 2020-11-22 03:52

    I now did the following.

    I declared a style inheriting everything from my general style and then disabling the titleBar.

    <style name="generalnotitle" parent="general">
        <item name="android:windowNoTitle">true</item>
    </style>
    

    Now I can set this style to every Activity in which I want to hide the title bar overwriting the application wide style and inheriting all the other style informations, therefor no duplication in the style code.

    To apply the style to a particular Activity, open AndroidManifest.xml and add the following attribute to the activity tag;

    <activity
        android:theme="@style/generalnotitle">
    
    0 讨论(0)
  • 2020-11-22 03:52

    Create a theme as below.

     <!-- Variation on the Light theme that turns off the title -->
    <style name="MyTheme" parent="android:style/Theme.Black">
        <item name="android:windowNoTitle">true</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 03:52

    This Solved my problem

    In manifest my activity:

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".SplashScreen">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
    

    In style under "AppTheme" name:

    <resources>
    
        <!-- Base application theme. -->
        <style name="AppTheme"
            parent="Theme.AppCompat.Light.NoActionBar">
            **<item name="windowActionBar">false</item>
            <item name="android:windowNoTitle">true</item>**
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
    
    </resources>
    
    0 讨论(0)
提交回复
热议问题