在 Xamarin.Android 中隐藏标题栏 [英] Hide Title Bar In Xamarin.Android

查看:50
本文介绍了在 Xamarin.Android 中隐藏标题栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用 Xamarin.即使在 VS 2019 中也花了一天时间修复愚蠢的调试问题.无论如何,我想所有阅读这篇文章的 Xamarin 开发人员都已经知道 ar$e Xamarin 是多么痛苦.

无论如何,在我的 Xmarin.Android(空白)项目中,我试图删除显示应用程序/项目名称的标题栏(我猜是这样).所以,这就是我要说的:

请注意:

<块引用>

在我继续说我的尝试之前,我必须补充一点,所有的解决方案都是从 Xamarin.Forms 相关的帖子/论坛中找到的.我很困惑,因为我选择 Xamarin.Android 是否做出了错误的选择,因为它几乎没有可用的支持.

到目前为止我尝试过的:

  • 在 AndroidManifest.xml 中添加了 android:theme="@android:style/Theme.NoTitleBar"

  • MainActivity.cs中添加RequestWindowFeature(WindowFeatures.NoTitle);,在OnCreate方法内,在调用base之前.OnCreate.

结果还是一样.标题栏始终存在.但是,我发现了 Xamarin 的另一个神秘错误(啊,没什么新东西吧?)!

从设计器的顶部栏更改主题时:

更改会反映在设计器中.我的意思是,如果我将主题更改为 Black NoTitleBar,标题栏就会消失.但是一旦我启动应用程序,标题栏就会在模拟器以及设计器中弹出.

所以,是这样吗?是否有任何 Xamarin.Android 特定解决方案?或者我真的做出了一个错误的决定,因为它缺乏支持论坛而选择 Xamarin.Android?

编辑 1

当我手动更改清单文件并设置 android:theme="@android:style/Theme.NoTitleBar",然后从设计器中,我将主题更改为 AppDefault,清单文件似乎保持不变.我的意思是在清单文件中声明的主题仍然是 Theme.NoTitleBar.

这就是 Activity 属性的样子:

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

Changing AppThemeTheme.NoTitleBar 会抛出运行时异常,说 No resource found with the given name (Theme.NoTitleBar).......

Xamarin 更新 - 更新 1.0

所以我发布这个问题已经大约一年了.在这段时间里,我与 Xamarin 进行了广泛的合作,包括 Xamarin Native 和 Xamarin Forms.所以我想分享我对 Xamarin 的看法.所以让我们先从优点开始:

Xamarin 原生如果您想要从事自由项目并希望自己处理后端和应用程序开发,那么 Xamarin Native 实际上是一个不错的选择(如果您来自 .Net 堆栈).在开始抨击我之前,请注意,我是一名高级工程师,曾参与过 Java 中的 android 原生开发、Swift 中的 iOS、React Native 中的跨平台以及 Xamarin.所以回到我原来的地方,Xamarin 实际上是一个了不起的工具.Xamarin 的 Native 部分实际上与实际的 Native 开发没有什么不同.从构建您梦想中的 UI,到编写最佳的应用内图像缓存库,再到构建性能最佳的应用,您都可以使用 Xamarin 本机来完成所有工作.

Xamarin 表单现在我已经看过很多关于 Xamarin Native 与 Forms 的教程,他们最大的论点是您可以使用 Xamarin Native 构建 native UI,但是而不是表单.作为一名 UI 设计师,我很幸运,我可以肯定地说,使用 Xamarin Native 来证明这是一种荒谬的逻辑.即使在 Xamarin Forms 中,您也可以构建外观最好的 UI.从自由职业者到企业项目,如果您想节省大量资金,并且如果您想要一款外观精美且具有出色的 REST API 支持后端的应用程序,所有这些都由 C# 提供支持,Xamarin Forms 是最佳选择.对此几乎没有任何争论.如果你想提出任何论点,我强烈建议你指出一些支持你论点的数据.

无论如何,正如我简短地提到优点,让我谈谈缺点(是的,我知道,非 C# 社区现在应该开始微笑了).下面是使用 Xamarin 的主要缺点,包括原生和表单:

<块引用>

  1. 你不是修复你的错误,而是修复已经存在的错误

我的!这是b**t中最大的痛苦!如果您是两年前在 Visual Studio 2017 中开始使用 Xamarin 的人,我认为您最能与之相关.我真的不得不花费无数个小时来获得一个Hello World".应用程序正在运行,因为你猜怎么着?我必须修复 Xamarin 中预先存在的错误.现在不要对 Visual Studio 是最好的 IDE 的事实感到困惑,因为它可以指出错误的来源.相信我,它不能!!!.

这还不是最糟糕的部分.最糟糕的是,即使是 Xamarin 社区也帮不了你!如果他们实际上不存在,他们怎么能哈哈?...这让我进入下一点:

<块引用>

  1. 一个小到需要眼镜才能看到的社区!

很抱歉这么说,但这个问题已经问了大约一年了.你在下面看到了多少写得很好的答案?不幸的是,我看到 2 个答案几乎没有解释!好吧,不是为了伤害回答者的感情,但事实就是如此.即使您在 Xamarin 的官方网站上寻求帮助,您甚至会很幸运地得到您能否发布整个异常日志?"之类的回复.因此,对于一个非常小的社区,在初始阶段,您实际上是靠自己的 :(

<块引用>

  1. 数以百万计的图书馆,但不是有用的.

是的,使用 C#,您可以访问十亿个库.但是当涉及到原生 Android 和 iOS 时,Xamarin 确实让你觉得有障碍:(

我记得我曾经需要类似于 Picasso 的东西来缓存图像,但 Xamarin 的最佳选择是 FFImageLoading.图书馆很好,但离毕加索也不近.我第一次安装并运行它以从网络获取大量图像时,它使我的整个应用程序崩溃,甚至没有指出导致崩溃的原因!!...这让我想到:

<块引用>

  1. 不仅仅是你,连 IDE 都一无所知

是的,当您的应用程序崩溃时,IDE 只是向您展示了冰山一角:( 您会抛出不明确的异常,例如 Java exited with code 1 和 bla-bla!但是当您询问 IDE 是什么导致了它,IDE 就像:嗯,我不知道伙计.谷歌是什么?".然后你跑到谷歌.你搜索这个问题,你会发现它确实是相关的到一些你甚至还没有接触过的东西!但猜猜是什么真正解决了这个问题?删除你安装的最后一个库或删除你写的最后一行,哇!它又可以工作了!!!

现在我想我已经讲得够多了,是时候鼓励您使用 Xamarin.让我给你一些我遵循的技巧,它们肯定会帮助你在 Xamarin 上步入正轨:

<块引用>

  1. Android Java 的搜索问题,而不是 Xamarin.Android

在大多数情况下,您可以通过在 google 中输入您的问题而不是提及in/for Xamarin.Android"来找到最佳解决方案.使用术语android java",因为这就是 Xamarin 编译的结果.Native android 拥有更大的社区,因此您找到解决方案的机会要高得多!是的,有时您会发现您很难找到完美的 C# 语法的 Java 代码,但是 Xamarin 官方网站上的 xamarin 社区已经为您写下了其中的大部分内容,在他们的回答中.因此,一旦您获得了不知道如何用 C# 编写的 Java 代码,只需搜索代码行并添加Xamarin"即可.在搜索查询的末尾.

<块引用>

  1. 使用 Android Studio 和 XCode 进行设计

永远,我重复永远,使用 Xamarin 设计器,特别是在 Android 上.它是如此具有误导性,即使您在 XML 中编写了正确的语法,它也会向您显示红线,而当您实际上写错了一些东西时,它完全符合但在启动时(或在指定的屏幕上)崩溃,让您几乎一无所知.所以总是使用 Android Studio 和 XCode 进行设计.创建 drawables,在 Android Studio 中的片段或视图中引用它们并创建您梦想中的设计.一切顺利后,只需将其中的文件/代码复制粘贴回 Visual Studio,即可节省大量麻烦和时间!但是,Xamarin iOS 设计器似乎确实比 android 设计器好一点.但是每当它向您抛出一些随机错误时,只需从 .xib 或故事板文件中复制粘贴代码并在 XCode 中打开它,您将更好地了解发生了什么!

<块引用>

  1. 清理、重建、启动、崩溃、重启电脑、清理、重建、启动成功!

是的,这就是 99% 的新人不知道的秘密!但我希望你现在知道了;-)

<块引用>

  1. 构建自己的库

是的,我知道你找到了你一直在寻找的最好的库,下载量超过一百万,但如果它使你的应用程序意外崩溃,是时候编写你自己的库了!它不会是最佳的,但它会帮助您提高编码技能,并且您将完全控制正在发生的事情.这个建议来自一个人,他不得不花一周时间为 Xamarin 和任何由 ​​C# 提供支持的平台编写一个惊人的图像缓存库(我想有一天在 Nuget 商店发布).

<块引用>

  1. 使用最新的 Visual Studio

花点时间下载包含所有最新更新的最新 Visual Studio(在撰写本文时为 2019 年).VS 2019 为 Xamarin 修复了大量错误,甚至设计人员现在也能很好地工作!所以继续,更新!!

哦,还有一个更大的问题:为什么使用 Xamarin Forms 而不是 React Native还是 Flutter 或其他什么?

嗯,唯一的答案是:使用 Xamarin,您只需要编写 C#,使用其他工具,您最终会编写大量 Java 和 Swift 代码.

无论如何,让我们谈谈未来!!

所以,老实说,Xamarin 从未引领市场.您会发现一些使用 Xamarin 构建的大型应用程序,但数量很少!但是,如果您不通过一项技术所支持的项目数量来判断一项技术,那么您就会明白 Xamarin 实际上是一个游戏规则改变者!你可以用它构建任何东西.但是,我仍然建议您尝试使用 React Native 或 Flutter,因为如果您了解多种技术,您就会更清楚在什么时候、什么情况下选择什么.所以祝你未来好运!选择您的技术并开始摇摆不定!

干杯!!

解决方案

嗯,所以它毕竟是一行修复?

任何人都在为这个问题苦苦挣扎:活动类 MainActivity.cs 中的以下更改修复了它:

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

我想你也可以设置其他主题.毕竟,您是 UI 高手!

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).......

Update on Xamarin - Update 1.0

So it's been about a year since i posted this question. In this time, i have had worked extensively with Xamarin, both Xamarin Native and Xamarin Forms. So i'm thinking of sharing my views on Xamarin. So let's start with the pros first :

Xamarin Native If you're someone who wants to take freelance projects and wants to handle both the backend as well as the app development yourself, then Xamarin Native is actually a great choice(if you come from the .Net stack). Before you start bashing me, please note that i'm a senior engineer who has worked with android native development in Java, iOS in Swift, cross-platform in React Native along with Xamarin. So getting back to where i was, Xamarin is actually an amazing tool. The Native part of Xamarin is literally no different than the actual Native development. From building you dream UI, to writing the best in-app image caching libraries, to building the best performing app, you can do it all with Xamarin native.

Xamarin Forms Now i've seen a lot of tutorials on Xamarin Native vs Forms and their biggest argument is that you can build native UIs with Xamarin Native but not with Forms. As i've had a great fortune working as a UI designer, i can say for sure that it's an absurd logic to justify using Xamarin Native. Even in Xamarin Forms, you can build the best looking UI. From freelance to enterprise projects, if you want to save a lot of money and if you want an amazing looking app with a great REST API supported backend, all powered by C#, Xamarin Forms is the best way to go. There's literally no arguments on that. If you want to put forward any arguments, i highly suggest you to point to some data as well that support your arguments.

Anyways, as i mentioned the pros in a short manner, let me talk a bit about the cons(Yeah, i know, the non-C# community should start smiling by now). So here're the top cons of using Xamarin, both native and forms:

  1. You don't fix your bugs, but rather the bugs that already exist there

My my! This is the biggest pain in the b**t! If you're someone who started off with Xamarin two years back, in Visual Studio 2017, i think you can relate the most to it. I literally had to spend countless hours just to get a "Hello World" app running, because guess what? I had to fix the bugs pre-existing in Xamarin. Now don't get baffled with the fact that Visual Studio is the best IDE so it can point out where the bug is coming from. Believe me, IT CAN'T!!!.

That's not the worst part tho. The worst part is, even the Xamarin community can't help you! How can they, if they don't actually exist lol ? ... which brings me to the next point :

  1. A community so small that you literally need glasses to see!

Sorry to put it that way, but this question is about a year old. How many well-written answers do you see below? I see 2 answers with little to no explanation unfortunately! Well, not to hurt the feelings of those who answered, but that's the truth. Even if you seek help on Xamarin's official site, you'ld be lucky even to get a response like "Can you post the entire exception log ?". So with a very small community, on an initial stage, you're literally on your own :(

  1. Millions of libraries, yet not the useful ones.

Yes, with C#, you get access to a billion of libraries. But when it comes to native Android and iOS, Xamarin literally makes you feel handicapped :(

I remember that i once needed something similar to Picasso for image caching but the best option Xamarin had was FFImageLoading. The library is good but not even close to Picasso. The first time i installed and ran it to get a number of images from the web, it crashed my entire app, without even pointing to what caused the crash!!... which brings me to :

  1. It's not just you, but even the IDE is clueless

Yes, when your app crashes, the IDE just shows you the tip of the ice-berg :( You are thrown ambiguous exceptions like Java exited with code 1 and bla-bla! But when you ask the IDE what caused it, the IDE be like : "Meh, i don't know dude. What is google for?". Then you run to google. You search up the issue and you see that it is literally related to something that you haven't even touched yet! But guess what actually fixes the issue? Remove the last library your installed or remove the last line you wrote and wallah! it works again!!!

Now i guess i've talked enough and it's time to encourage you to use Xamarin. Let me give you some tips i followed, which will definitely help you be on track with Xamarin :

  1. Search issues with Android Java, not with Xamarin.Android

For the most cases, you can find the best solution by typing your issue in google and instead of mentioning "in/for Xamarin.Android" use the term "android java", because that's what Xamarin compiles down to. The Native android has a much bigger community, so you have much higher chances of finding your solution! Yes, sometimes you'll find Java code that you'ld have hard time to find the perfect C# syntax for, but the xamarin community on the official Xamarin site has most of it written out, in their answers, for you. So once you get a java code you don't know how to write in C#, just search up the line of code and add "Xamarin" in the end of your search query.

  1. Use Android Studio and XCode for design

Never ever, i repeat never ever, use the Xamarin designer, specially on Android. It is so misleading that even if you write a correct syntax in XML, it shows you red lines, and when you actually actually write something wrong, it complies perfectly but crashes on launch(or on the specified screen) leaving you mostly clueless. So always use Android Studio and XCode for designing. Create the drawables, reference them in your fragments or views within Android Studio and create your dream design. When all is good, just copy paste the files/codes in them back to Visual Studio and it'll save of tons of headache and time! However, the Xamarin iOS designer does seem to work a bit better than the android one. But whenever it throws you some random error, just copy paste the code from the .xib or storyboard file and open it in XCode, you will have much better understanding of what's going on!

  1. Clean, rebuild, launch, crash, restart PC, clean, rebuild, launch success!

Yep, that's the secret 99% newcomers don't know! But i hope you know it now ;-)

  1. Build your own libraries

Yes, i know you found the best library you've been looking for and it has a million+ downloads, but if it makes your app crash unexpectedly, it's time to write your own library! It's not gonna be optimal, but it'll help you sharpen your coding skills and you'll have full control on what's going on. And this advice is coming from someone who had to spend a week writing an amazing image caching library for not just Xamarin but for any platform powered by C#(I'm thinking of releasing on the Nuget store someday).

  1. Use the latest Visual Studio

Spend your time and download the latest Visual Studio(2019, at the time of writing this) with all the latest updates. VS 2019 has tons of bug fixes for Xamarin and even the designers work pretty well now! So go on, update!!

Oh, and for the bigger question: Why use Xamarin Forms instead of React Native or Flutter or anything else?

Well, the one line answer is : With Xamarin, you only need to write C#, with others, you end up writing a lot of Java and Swift code.

Anyways, let's talk a bit about THE FUTURE!!

So, to be honest, Xamarin never led the market. You will find some big apps built with Xamarin but the numbers are small! But if you don't judge a technology by the number of projects it powers, then you'ld understand that Xamarin is actually a game changer! You can build anything and everything with it. However, i'ld still suggest you try out React Native or Flutter, because if you know multiple technologies, you have better knowledge of what to choose when, under what circumstances. So best of luck for your future! Pick your tech and start rocking!

Cheers!!

解决方案

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!

这篇关于在 Xamarin.Android 中隐藏标题栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆