本文介绍了Android 定位更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Activity 叫做 SingleTouchTest 来理解屏幕触摸.奇怪的是 SingleTouchTest 以我所处的任何方向开始,但旋转设备不会导致屏幕旋转.

I have a simple Activity called SingleTouchTest to make sense of screen touches. What is strange is that SingleTouchTest starts in whatever orientation I'm in but rotating the device does not result in screen rotation.

我的测试设备是运行 Android 4.0.3 的 Acer A100.

My test device is an Acer A100 running Android 4.0.3.

主要的Activity 包含一个ListView,它导航到我的测试Activityes,包括SingleTouchTest.除了旋转之外,我可以毫无问题地运行 SingleTouchTest(完整代码如下).在 AndroidManifest.xml(下面的完整代码)中,我尝试了

The main Activity contains a ListView that navigates to my test Activityes, including SingleTouchTest. I can run SingleTouchTest (full code below) without problems except rotation. In AndroidManifest.xml (full code below) I have tried every combination of

android:configChanges="keyboard|keyboardHidden|orientation"android:screenOrientation="unspecified"

并且它不会自动旋转.我什至从 SingleTouchTest 中删除了 onConfigurationChanged() 方法,但没有任何反应.

and it does not auto rotate. I even removed the onConfigurationChanged() method from SingleTouchTest and nothing happens.

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

     }

}

推荐答案

既然你正在使用

 android:screenOrientation="unspecified"

对于每个活动,它根据谷歌定义为

for each activity and it is defined according to google as

默认值.系统选择方向.政策吧用途,因此在特定情况下做出的选择可能会有所不同从设备到设备.

这让我认为设备出于某种原因正在声明所需的方向.也许尝试将屏幕方向切换到

Which makes me think that the device is declaring the desired orientation for some reason. Maybe try switching the screenorientation to

android:screenOrientation="fullSensor"

这应该让它脱离特定设备的手.

that should take it out of the specific devices hands.

这篇关于Android 定位更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 12:40