Android + XAMARIN + 强制屏幕停留在“纵向"状态模式(使用 AndroidManifest) [英] Android + XAMARIN + Force screen to stay in "Portrait" mode (using AndroidManifest)

查看:40
本文介绍了Android + XAMARIN + 强制屏幕停留在“纵向"状态模式(使用 AndroidManifest)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里阅读了这个问题的一些答案,但不知何故我没有让它起作用.

我的 AndroidManifest.xml 如下所示:

<使用-sdk/><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><应用程序 android:label="WQS" android:icon="@drawable/temporaryIcon"><activity android:name=".MainActivity"android:configChanges="keyboard|keyboardHidden|orientation"/></应用程序></清单>

我仍然可以切换到横向模式,因为布局在横向模式下变得非常难看,所以我想避免这种情况.非常感谢帮助.

可能很重要:我正在使用最新的 Xamarin 开发应用

解决方案

您可以使用 class 属性将您的 activity 标记为 portrait:

[Activity (Label = "MyMainScreen", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]公共类 Activity1:活动

由于 Xamarin 自动生成类名,而无需对类名进行硬编码,这是强制 Activity 始终基于肖像的唯一方法.

更新:

我收到了很多关于这个 Q/A 的 IM 问题,所以我正在扩展关于为什么使用 manifest 方法不起作用的答案.

Xamarin 会为您的活动自动生成一个完全限定的类名,这些活动没有分配给您的活动类的 Name 属性.

如果您查看来自 Xamarin Android .apk 的签名/生成清单你会看到一个类名,如:

md5d2519388ea1895e3e3594794d2e0c4ce.MainActivity

因为您使用点名称类标识符标记清单,该标识符将获得包名称前缀:

完全限定类名不匹配,因为以句点开头的类名将获得包名前缀,而那些永远不会匹配自动生成的类名.

您可以通过使用 Activity 属性中的 Name 元素来解决此自动生成的完全限定类名称,以防止创建自动生成的名称,即:

[Activity(Label = "PlayScriptStarling", Name = "com.sushihangover.playscriptstarling.MyBigBadGameEveryOneShouldPlay", MainLauncher = true, Icon = "@mipmap/icon")]

现在,假设您的包名称是com.sushihangover.playscriptstarling",使用:

在您的清单中将用作类名,一旦在清单中展开,就会与您的代码中的名称相匹配.

问题是,如果您的包名称更改,您会破坏清单中的 点类名称.因此,使用 ScreenOrientation = ScreenOrientation.Portrait 是一种更清晰的方向分配方式,它不需要您为每个活动分配完全限定的类名,并且在包名称更改时不会中断.>

注意:Activity 类属性中的 Name 元素不支持 点类名,需要使用完全扩展的类名.这已作为功能请求提交给 Xamarin...

I read some of the answers to this problem on here but somehow I don't get it to work.

My AndroidManifest.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="mWidas2.mWidas2" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
  <uses-sdk />
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  <application android:label="WQS" android:icon="@drawable/temporaryIcon">
    <activity android:name=".MainActivity"
    android:configChanges="keyboard|keyboardHidden|orientation" />
  </application>
</manifest>

I am still able to switch to landscape mode and since the layout gets pretty ugly in landscape mode I want to avoid that. Help much appreciated.

Might be of any importance: I'm developing the App with the latest Xamarin

解决方案

You can tag your activity to be portrait using a class attribute:

[Activity (Label = "MyMainScreen", MainLauncher = true, ScreenOrientation = ScreenOrientation.Portrait)]
public class Activity1 : Activity

This is the only way that will force the activity to always be portrait based due to auto-generated class names produced by Xamarin without hard-coding your class names.

Update:

I've gotten a lot of IM questions concerning this Q/A, so I'm expanding the answer on why using the manifest method does not work.

Xamarin auto-generates a fully qualified class name for your activities that do NOT have a Name attribute assigned to your activity class.

If you look at a signed/generated manifest from an Xamarin Android .apk you will see a class name like:

md5d2519388ea1895e3e3594794d2e0c4ce.MainActivity

Since you are tagging your manifest with a dot name class identifier that will get the package name prefixed to it:

<activity android:name=".MainActivity"

The fully qualified class names do not match since a class name that begins with a period will get prefix with the package name and those never match the auto-generated class name.

You can work around this auto-generated fully qualified class name by using the Name element in your Activity attribute to prevent the auto-generated name from being created, i.e.:

[Activity(Label = "PlayScriptStarling", Name = "com.sushihangover.playscriptstarling.MyBigBadGameEveryOneShouldPlay", MainLauncher = true, Icon = "@mipmap/icon")]

Now, assuming your package name is "com.sushihangover.playscriptstarling", using:

<activity android:name=".MyBigBadGameEveryOneShouldPlay"

in your manifest will work as the class name, once expanded in the manifest matches the one in your code.

The issue is then if your package name changes you break your dot class names in your manifest. So using ScreenOrientation = ScreenOrientation.Portrait is a cleaner way of assigning orientation that does not require you to assign fully qualified class name on every one of your activities and does not break if the package name changes.

Note: The Name element within the Activity class attribute does NOT support dot class names and requires a fully expanded class name to be used. This has been submitted to Xamarin as a feature request...

这篇关于Android + XAMARIN + 强制屏幕停留在“纵向"状态模式(使用 AndroidManifest)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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