强制“肖像”定向模式
我试图强制我的应用程序使用“纵向”模式,因为我的应用程序绝对不是为“横向”模式设计的。
阅读了一些论坛后,我在清单文件中添加了这些行:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:screenOrientation="portrait">
但它在我的设备(HTC Desire)上不起作用。它从“纵向”切换到“横向”,忽略清单文件中的行。
在阅读更多论坛后,我尝试将其添加到我的清单文件中:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
并将此函数添加到我的活动类中:
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
但同样,没有运气。
I'm trying to force the "portrait" mode for my application because my application is absolutely not designed for the "landscape" mode.
After reading some forums, I added these lines in my manifest file:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:screenOrientation="portrait">
But it doesn't work on my device (HTC Desire). It switches from "portrait" lo "landscape", ignoring the lines from the manifest file.
After more forums reading, I tried to add this in my manifest file:
<application
android:debuggable="true"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:configChanges="orientation"
android:screenOrientation="portrait">
and this function in my activity class:
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
But again, no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
不要将方向应用于 application 元素,而应将属性应用于 activity 元素,并且还必须将
configChanges
设置为下面注明。示例:
这应用于清单文件
AndroidManifest.xml
中。Don't apply the orientation to the application element, instead you should apply the attribute to the activity element, and you must also set
configChanges
as noted below.Example:
This is applied in the manifest file
AndroidManifest.xml
.请注意,它
被添加到定义活动的清单文件中。
Note that
is added in the manifest file - where the activity is defined.
如果您的应用程序中有很多像我这样的活动,或者如果您不想在清单中输入每个活动标记的代码,您可以执行此操作。
在您的应用程序基类中,您将获得生命周期回调,
因此基本上,在应用程序类中创建创建时触发的每个活动中发生的事情是代码..
我希望这会有所帮助。
If you are having a lot activity like mine, in your application Or if you dont want to enter the code for each activity tag in manifest you can do this .
in your Application Base class you will get a lifecycle callback
so basically what happens in for each activity when creating the on create in Application Class get triggered here is the code ..
i hope this helps.
我认为
android:screenOrientation="portrait"
可用于个人活动。因此,在
标记中使用该属性,如下所示:I think
android:screenOrientation="portrait"
can be used for individual activities. So use that attribute in<activity>
tag like :设置强制纵向或横向模式,分别添加线条。
导入下方行:
在
setContentView(R.layout.activity_main);
上方添加下方行对于肖像:
对于景观:
这肯定会起作用。
Set force Portrait or Landscape mode, Add lines respectively.
Import below line:
Add Below line just above
setContentView(R.layout.activity_main);
For Portrait:
For Landscap:
This will definitely work.
根据 Android 的文档,您还应该经常包含
screenSize
作为可能的配置更改。另外,如果您的示例中都包含值
keyboardHidden
,那么您是否还应该考虑locale
、mcc
、fontScale,<代码>键盘和其他?..
According to Android's documentation, you should also often include
screenSize
as a possible configuration change.Also, if you all include value
keyboardHidden
in your examples, shouldn't you then also considerlocale
,mcc
,fontScale
,keyboard
and others?..我在 AndroidManifest.xml 中有这一行,
我将其更改为(刚刚添加
android:screenOrientation="portrait"
),这为我解决了问题。
I had this line in my AndroidManifest.xml
Which I changed to (just added
android:screenOrientation="portrait"
)This fixed things for me.
补充一下:我最近更新了一个应用程序,之前的版本可以在横向和纵向模式下工作,我希望更新后的版本应该在纵向模式下工作,所以我添加
到相应的活动中,当我测试时它就崩溃了更新。然后我
也添加了,并且有效。
Something to complement: I have updated an app recently, the previous was working in both landscape and portrait mode, and I want the updated version should work in portrait mode, so I added
to the corresponding activity, and it just crashed when I tested the update. Then I added
too, and it works.
如果您希望在
debug
和release
版本中支持不同的方向,请如此编写(请参阅https://developer.android.com/studio/build/gradle-tips#share-properties-with-the-manifest)。在
app
文件夹的build.gradle
中写入:然后在
AndroidManifest
中,您可以在任何Activity
中使用此变量“orientation” code>:添加
android:configChanges
:您可以在调试中
manifestPlaceholders = [configChanges: "",orientation: "fullSensor"]
和manifestPlaceholders = [configChanges: " KeyboardHidden|orientation|screenSize",orientation: "portrait"]
在发布中,If you wish to support different orientations in
debug
andrelease
builds, write so (see https://developer.android.com/studio/build/gradle-tips#share-properties-with-the-manifest).In
build.gradle
of yourapp
folder write:Then in
AndroidManifest
you can use this variable "orientation" in anyActivity
:You can add
android:configChanges
:manifestPlaceholders = [configChanges: "", orientation: "fullSensor"]
in debug andmanifestPlaceholders = [configChanges: "keyboardHidden|orientation|screenSize", orientation: "portrait"]
in release,我想您想将
android:configChanges="orientation|keyboardHidden"
添加到您的 Activity 中?否则,活动将在配置更改时重新启动。那么onConfigurationChanged
不会被调用,只会调用onCreate
I think you want to add
android:configChanges="orientation|keyboardHidden"
to your activity? Otherwise the activity is restarted on config-change. TheonConfigurationChanged
would not be called then, only theonCreate
要使您的整个应用程序在完整的纵向模式或完整的横向模式下运行,您必须做一件非常简单的事情。
android:screenOrientation="portrait"
这将使您的整个应用程序在纵向模式下运行,或者您也可以将其设置为在横向模式下运行。
To make your whole app run on complete Portrait mode or complete Landscape mode you have to do a very simple thing.
android:screenOrientation="portrait"
This will make your whole app run on Portrait mode or you can also set it to run on Landscape mode.
简短的回答:不要这样做。
重新设计您的应用程序,使其可以在纵向和横向模式下运行。没有什么 UI 不能被设计为同时支持纵向和横向工作;只有懒惰或缺乏想象力的开发人员。
原因其实很简单。您希望您的应用程序可供尽可能多的受众在尽可能多的不同设备上使用。通过强制使用特定的屏幕方向,您可以阻止您的应用程序在不支持该方向的设备上(可用)运行,并且您会挫败和疏远喜欢不同方向的潜在客户。
示例:您将应用程序设计为强制使用纵向模式。客户在二合一设备上下载该应用程序,他们主要在横向模式下使用该设备。
后果 1:您的应用程序无法使用,或者您的客户被迫拔出设备、旋转设备并以他们不熟悉或不舒服的方向使用设备。
后果 2:客户对应用程序的非直观设计感到沮丧,并找到替代方案或完全放弃该应用程序。
我现在正在用一个应用程序来解决这个问题,作为消费者和开发者,我讨厌它。尽管该应用程序非常有用,尽管它提供的功能非常出色,但我绝对讨厌该应用程序,因为它迫使我使用与我使用设备的所有其他方式相反的方向。
您不希望您的客户讨厌您的应用程序。
我知道这并不能直接回答问题,所以我想为那些好奇的人更详细地解释一下。
开发人员往往非常擅长编写代码,但不擅长设计。这个问题虽然听起来像是一个代码问题,而且提问者也确实感觉这是一个代码问题,但它实际上是一个设计问题。
问题实际上是“我应该在应用程序中锁定屏幕方向吗?”提问者选择将 UI 设计为仅在纵向模式下才能正常运行且看起来良好。我怀疑这是为了节省开发时间,或者因为应用程序的工作流程特别有利于纵向布局(移动游戏中常见)。但这些原因忽略了促进正确设计的所有真正重要因素。
客户参与度 - 您希望客户感觉被吸引到您的应用中,而不是被推出。应用程序应该从客户打开应用程序之前所做的任何事情顺利过渡。 (这就是大多数平台具有一致的设计原则的原因,因此大多数应用程序看起来或多或少相似,尽管它们不一定如此。)
客户响应 - 您希望客户对您的应用程序做出积极反应。他们应该喜欢使用它。即使它是一个工作工资应用程序,他们也应该很高兴打开它并打卡。该应用程序应该节省客户的时间并减少对替代方案的挫败感。 (惹恼用户的应用程序会对您的应用程序产生怨恨,进而发展为对您的品牌的怨恨。)
客户转化 - 您希望您的客户能够快速轻松地从浏览转向互动。将展示次数转化为收入,这是任何应用程序的最终目标。 (从业务角度来看,不产生收入的应用程序是在浪费您的时间。)
设计不当的 UI 会降低客户参与度和响应,最终导致收入下降。在以移动设备为中心的世界中(特别是在纵向/横向显示模式方面),这解释了为什么响应式网页设计如此重要。 沃尔玛加拿大公司在2013 年 11 月,他们的网站上线,客户转化率提高了 20%。 O'Neill Clothing 实施了响应式网页设计和 使用 iOS 设备的客户的收入增加了 101.25%,并且 < strong>591.42%来自使用 Android 设备的客户。
开发人员还倾向于专注于实现特定的解决方案(例如锁定显示方向),并且本网站上的大多数开发人员都会非常乐意帮助实现该解决方案,而不会质疑这是否是最好的解决方案问题的解决方案。
锁定屏幕方向在 UI 设计中相当于实现 do-while 循环。您真的确定要这样做,还是有更好的选择?
不要强迫您的应用程序进入单一显示模式。投入额外的时间和精力来使其响应迅速。
Short answer: Don't do it.
Redesign your app so that it can run in both portrait and landscape mode. There is no such thing as a UI that can't be designed to work in both portrait and landscape; only lazy or unimaginative developers.
The reason why is rather simple. You want your app to be usable by as wide an audience as possible on as many different devices as possible. By forcing a particular screen orientation, you prevent your app from running (usably) on devices that don't support that orientation and you frustrate and alienate potential customers who prefer a different orientation.
Example: You design your app to force portrait mode. A customer downloads the app on a 2-in-1 device which they use predominantly in landscape mode.
Consequence 1: Your app is unusable, or your customer is forced to undock their device, rotate it, and use it in an orientation that is not familiar or comfortable for them.
Consequence 2: The customer gets frustrated by your app's non-intuitive design and finds an alternative or ditches the app entirely.
I'm fighting with this with an app right now and as a consumer and a developer, I hate it. As useful as the app is, as fantastic as the features are that it offers, I absolutely hate the app because it forces me to use an orientation that is counter to every other way that I use my device.
You don't want your customers to hate your app.
I know this doesn't directly answer the question, so I want to explain it in a little more detail for those who are curious.
There is a tendency for developers to be really good at writing code and really terrible at design. This question, though it sounds like a code question and the asker certainly feels like it's a code question, is really a design question.
The question really is "Should I lock the screen orientation in my app?" The asker chose to design the UI to function and look good only in portrait mode. I suspect it was to save development time or because the app's workflow is particularly conducive to a portrait layout (common for mobile games). But those reasons neglect all the real important factors that motivate proper design.
Customer engagement - you want your customers to feel pulled into your app, not pushed out of it. The app should transition smoothly from whatever your customer was doing prior to opening your app. (This is the reason most platforms have consistent design principles, so most apps look more or less alike though they don't have to.)
Customer response - you want your customers to react positively to your app. They should enjoy using it. Even if it's a payroll app for work, it should be a pleasure for them to open it and clock in. The app should save your customers time and reduce frustration over alternatives. (Apps that annoy users build resentment against your app which grows into resentment against your brand.)
Customer conversion - you want your customers to be able to quickly and easily move from browsing to interacting. This is the ultimate goal of any app, to convert impressions into revenue. (Apps that don't generate revenue are a waste of your time to build, from a business perspective.)
A poorly designed UI reduces customer engagement and response which ultimately results in lower revenue. In a mobile-centric world (and particularly on the subject of portrait/landscape display modes), this explains why responsive web design is such a big deal. Walmart Canada introduced responsive design on their website in November 2013 and saw a 20% increase in customer conversion. O'Neill Clothing implemented responsive web design and revenue from customers using iOS devices increased 101.25%, and 591.42% from customers using Android devices.
There is also a tendency for developers to focus intently on implementing a particular solution (such as locking display orientation), and most of the developers on this site will be all too glad to help implement that solution, without questioning whether that is even the best solution to the problem.
Locking your screen orientation is the UI design equivalent of implementing a do-while loop. Are you really sure you want to do it that way, or is there a better alternative?
Don't force your app into a single display mode. Invest the extra time and effort to make it responsive.