【问题标题】:Android App to always stay in LandscapeAndroid 应用程序始终保持在横向
【发布时间】:2016-10-25 03:33:36
【问题描述】:

我查看了Here,但仍然无法解决我的问题。

我最近跳入了 android (headfirst) 并且一直在搞乱。无论屏幕以哪种方式倾斜,我都希望我的应用程序仅在横向模式下运行。

我的主屏幕基本上是一个带有背景的页面和几个在右侧对齐的按钮。

这是我的 XML

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft = "450px"
    android:paddingRight = "60px"
    android:paddingTop="25px"
//I have tried adding orientation commands here like the one below??
    android:screenOrientation="landscape"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background = "@drawable/sign"
    >

<Button 
android:id="@+id/Button1" 
android.layout_height="60px"
android.layout_width="30px"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:text="Button1"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

<Button 
android:id="@+id/Button2" 
android.layout_height="60px"
android.layout_width="30px"
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:text="Button2"/>

【问题讨论】:

  • bahh 谢谢大家,我更改了错误的文件。我链接到的另一个问题本来可以解决问题

标签: android xml android-layout


【解决方案1】:

在 manifest.xml 文件中添加这一行。

<activity android:name=".activity" android:screenOrientation="portrait"></activity>

如果您只想要横向,则更改横向而不是纵向

看到这个How to set android show vertical orientation?

【讨论】:

  • 看来,无论这条线走到哪里,我都会收到一个错误,如果不是,它会在加载时崩溃。
【解决方案2】:

在清单文件中的所有活动中添加以下属性:

android:screenOrientation="landscape"

例如:

<activity  android:name=".MainActivity"  android:screenOrientation="landscape"  "  >
              <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

您可以在 application 标记中添加此属性,也可以在清单文件中添加。

希望这可以帮助您解决问题。

【讨论】:

    【解决方案3】:

    您需要将android:screenOrientation="landscape" 放在AndroidManifest.xml 文件中的活动标签上。

    参考:http://developer.android.com/guide/topics/manifest/activity-element.html

    【讨论】:

    • 这也是我尝试过的方法,但它一直给我一个错误。我再试一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    相关资源
    最近更新 更多