Hide Title Bar In Xamarin.Android

十年热恋 提交于 2020-01-03 14:44:30

问题


I just started out with Xamarin. Spent literally a day fixing stupid debugging issues even in VS 2019. Anyways, i guess all the Xamarin devs reading this post already knows how much of a pain in the ar$e Xamarin is.

Anyways, in my Xmarin.Android (Blank) project, i am trying to remove the title bar which says the app/project name(I guess so). So, here's what i'm talking about :

Please note :

Before i go on saying what i tried, i must add, all of the solutions were found from Xamarin.Forms related posts/forums. I am very confused thinking if i made the wrong choice by choosing Xamarin.Android as it has little to no support available.

What i tried so far :

  • Added android:theme="@android:style/Theme.NoTitleBar" in AndroidManifest.xml

  • Added RequestWindowFeature(WindowFeatures.NoTitle); in MainActivity.cs, inside OnCreate method, before calling base.OnCreate.

The result remains the same. The title bar is always there. However, here's another mysterious bug of Xamarin(Ah, nothing new, right?) i found!

When changing the theme from the designer's top bar :

the changes are reflected in the designer. I mean if i change the theme to, say, Black NoTitleBar, the title bar goes away. But as soon as i launch the app, the title bar pops up in the emulator as well as in the designer.

So, is this the way it is? Are there any Xamarin.Android specific solution for this? Or did i really make a bad decision choosing Xamarin.Android as it lacks support forums?

EDIT 1

When i change the manifest file manually and set android:theme="@android:style/Theme.NoTitleBar", then from the designer, i change the theme to AppDefault, the manifest file seems to remain the same. I mean the theme declared in the manifest file remains Theme.NoTitleBar.

And this is how the Activity attribute looks like :

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

Changing AppTheme with Theme.NoTitleBar throws a runtime exception saying No resource found with the given name (Theme.NoTitleBar).......


回答1:


Hmm, so it was a one line fix after all?

Anybody struggling with this issues: The following change in the activity class MainActivity.cs fixed it :

[Activity(Label = "@string/app_name", Theme = "@style/Theme.AppCompat.Light.NoActionBar", MainLauncher = true)]

You can set other themes as well, i guess. After all, you are the UI master!




回答2:


There are two methods to hide title bar in Xamarin.Android. One is already mentioned by Aousaf Rashid by using styles The other simple method is:

ActionBar.Hide();

in the oncreate method in activity




回答3:


[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

This work for me



来源:https://stackoverflow.com/questions/55520143/hide-title-bar-in-xamarin-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!