Android屏幕方向变化

5
我有一个简单的名为"SingleTouchTest"的Activity,用于理解屏幕触摸。奇怪的是,"SingleTouchTest"可以在任何方向下启动,但旋转设备并不会导致屏幕旋转。
我的测试设备是运行Android 4.0.3的Acer A100。
主Activity包含一个ListView,用于导航到我的测试Activities,包括"SingleTouchTest"。我可以运行"SingleTouchTest"(完整代码如下)没有问题,除了旋转外。在AndroidManifest.xml(完整代码如下)中,我尝试了每个组合:
android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"
但它不会自动旋转。我甚至从"SingleTouchTest"中删除了"onConfigurationChanged()"方法,但仍然没有效果。
完整的AndroidManifest.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest 
    package="com.Bespoke.AndroidBasics"
    android:versionCode="1"
    android:versionName="1.0"
    android:installLocation="preferExternal" xmlns:android="http://schemas.android.com/apk/res/android">

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="Android Basics">
        <activity
            android:name=".AndroidBasicsStarter"
            android:label="Android Basics"
            android:screenOrientation="unspecified">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LifeCycleTest"
            android:label="Life Cycle Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".SingleTouchTest"
            android:label="Single Touch Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".MultiTouchTest"
            android:label="Single Touch Test"
            android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
        <activity
            android:name=".KeyTest"
            android:label="Key Test" android:screenOrientation="unspecified"/>
    </application>

</manifest>

SingleTouchTest的完整代码如下:

package com.Bespoke.AndroidBasics;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SingleTouchTest extends Activity
                             implements OnTouchListener {

    StringBuilder builder = new StringBuilder();
    TextView textView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        textView = new TextView(this);
        textView.setText("Touch and drag (one finger only!)");
        textView.setOnTouchListener(this);
        this.setContentView(textView);
    }

    public boolean onTouch(View v, MotionEvent event) {
        builder.setLength(0);  // clear the builder
        switch(event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            builder.append("down, ");
            break;
        case MotionEvent.ACTION_MOVE:
            builder.append("move, ");
            break;
        case MotionEvent.ACTION_CANCEL:
            builder.append("cancel, ");
            break;
        case MotionEvent.ACTION_UP:
            builder.append("up, ");
            break;
        }
        builder.append(event.getX());
        builder.append(", ");
        builder.append(event.getY());
        String text = builder.toString();
        Log.d("TouchTest", text);
        textView.setText(text);
        return true;  // Consume the event, if false super.onTouch superceeds us
    }

     @Override
     public void onConfigurationChanged(Configuration  newConfig) {
       super.onConfigurationChanged(newConfig);

     }

}

我无法访问您在Pastebin上发布的链接。但我猜我的答案应该能解决问题。 - user1092042
3个回答

9

既然您在使用

 android:screenOrientation="unspecified"

对于每个活动,它根据Google的定义进行定义

默认值。系统选择方向。它使用的策略,因此在特定上下文中做出的选择可能因设备而异。

这让我想到设备以某种方式声明了所需的方向。也许尝试将屏幕方向切换到

android:screenOrientation="fullSensor"

这应该将其从特定设备的掌握中解脱出来。


我刚刚发现了一篇网帖,让我可以将我的HTC Incredible连接到ADB。我的手机上旋转功能正常,但在A100上却不行。我的其他应用程序都可以正常旋转,这是怎么回事? - uMinded
@uMinded 你试过改变方向吗? - MikeIsrael
HTC Incredible(2.3.3)使用以下代码旋转屏幕:<activity android:name=".SingleTouchTest" android:label="Single Touch Test" android:configChanges="keyboard|keyboardHidden|orientation"/> 但是A100(4.0.3)保持在父级方向中。我只能强制它为横向或纵向。 - uMinded
不过,如果您添加android:screenOrientation="fullSensor"会发生什么呢? - MikeIsrael
HTC按预期旋转,而A100则完全没有反应。唯一起作用的android:screenOrientation设置是portrait或landscape。 - uMinded
显示剩余3条评论

2
在 AndroidManifest 中删除 |orientation。原因如下: android:configChanges 允许您手动处理特定的配置更改。由于您没有通过 onConfigurationChanged() 中的代码告诉您的应用程序手动旋转,因此它根本不执行任何操作。为了使您的应用程序能够自动使用旋转功能,您必须告诉 Manifest 您不是手动处理方向。
有关说明,请参见 这里Android Developers Site

我刚刚发现了一篇网帖,让我可以将我的HTC Incredible连接到ADB。我的手机上旋转功能正常,但在A100上却不行。我的其他应用程序都可以正常旋转,这是怎么回事? - uMinded
@uMinded 如果只是特定于设备的问题,您可以检查设备的设置。它们可能也会中断方向更改。 - Fuzzical Logic
这完全取决于ROM(即制造商)。A100是由谁制造的? - Fuzzical Logic
宏碁推出A100(7" Tegra 2)版本,构建版本为Acer_AV041_A100_1.012.00_ww_GEN1,这是该设备的官方ICS更新。但由于在设置->关于->系统更新中无法找到更新,我手动进行了安装。 - uMinded
哇...我感觉自己像个工具,我刚把平板电脑放进了一个新的后市场保护套里,可能不小心按到了锁定按钮。我以为那是一个“屏幕锁定”按钮,就像我的MP3播放器一样,所以屏幕被锁定在“关闭”而不是横向。 - uMinded
显示剩余2条评论

2

试试这个:

android:screenOrientation="fullSensor"

设备方向传感器决定了方向。显示屏的方向取决于用户如何持设备。这应该是您想要的。此外,更多选项请查看此链接-Activity Element。

1
抱歉,我不知道怎么错过了你的回答,基本上写了相同的内容,所以删除了我的回答。 - MikeIsrael
我认为你不必删除它,因为你已经付出了自己的努力来写答案。这样的事情确实会发生。没关系。 - user1092042

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接