ChatGPT解决这个技术问题 Extra ChatGPT

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

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:style/Theme.NoTitleBar.

Using the NoTitleBar theme as a parent for my style would remove the title bar from all of my activities.

Can I set a no title style item somewhere?


Z
Ziem

Do this in your onCreate() method.

//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);

//Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

//set content view AFTER ABOVE sequence (to avoid crash)
this.setContentView(R.layout.your_layout_name_here); 

this refers to the Activity.


Great answer. You don't have to do both though, removing the notification bar may be overkill.
Be certain to put it before the call to setContentView(R.id.x), otherwise it will crash. The reason wasn't immediately obvious to me. 08-13 12:47:33.561 E/AndroidRuntime( 9125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.SplashActivity}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
You should really mention that when using requestWindowFeature(), a force-close will occur if you execute this line after adding any content. And on a side note, the XML method is much safer and it also separates page structure from your code, (which is the entire purpose of the XMLs in the first place.)
This doesn't work using API 22. I still see the title bar initially as the app boots.
it works if your activity extends Activity NOT AppCompatActivity i.e. your class should be declared as public class myActi extends Activity {}
s
schlenger

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.


I don't know why. I have only used the default project and added an activity. The autocompletion my not work, but the theme is there.
android:theme="@android:style/Theme.Black.NoTitleBar" is probably more useful unless you're making a game
Or