安卓设备的onConfigurationChanged事件无法处理方向问题

4

我有一个活动,在活动启动时我需要将方向调整为横向,然后稍后我想要处理设备旋转时的两个方向变化,但它只会更改一次方向,之后就不再更改方向。以下是我的代码,请帮忙:

    public class Hls extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hls);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);


}



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

    Log.v("new orientation", "yes");

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } 

}
3个回答

6
在onCreate中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)会永久将您的方向固定为横屏,避免任何方向变化。这就是为什么您的方向被固定且不响应旋转的原因。
以下是另一种可用的方法:
1> 在布局中创建两个视图,即横屏视图和竖屏视图。假设activity_hls_land和activity_hls_port。
2> 在onConfigurationChanged(Configuration newConfig)中使用setContentView(R.layout.activity_hls)代替setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)或setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
以下是示例代码:
 public class MainActivity extends Activity {
boolean isLaunched=true;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        if(isLaunched){
        Toast.makeText(this, "1 st launch " , Toast.LENGTH_SHORT).show(); 
        setContentView(R.layout.activity_hls_land);
        }

    }

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

    if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_port);
    }
     if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_hls_land );
    } 
    }

}

在清单文件中的活动中添加android:configChanges="orientation"来处理屏幕旋转问题:

<activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" 
            android:configChanges="orientation"
            >

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
          </activity>

activity_hls_port 的示例布局:

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
</LinearLayout>

横屏模式示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:orientation="vertical"
    android:rotation="90">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
 <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        tools:context=".MainActivity" />
</LinearLayout>

好的,谢谢。请告诉我如何设计横屏布局,也就是竖屏和横屏布局之间有什么区别。我的竖屏布局是:<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <VideoView android:id="@+id/player" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" /> </RelativeLayout> - Munazza
嗨!其实没有什么区别。你所需要做的就是创建一个横向方向的布局。 - Code Word
如何在布局文件中指定横向方向模式? - Munazza
只是横屏模式的外观而已。您需要在布局中按照横屏模式排列视图。 - Code Word
我已经添加了示例布局。试试看吧。 - Code Word

1

在这个声明中

if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_LONG).show();
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    } 

同样也在onCreate(Bundle bundle);中。

你调用了

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

或者

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

这意味着您的活动处于横向/纵向方向,并且不会再旋转

删除它将再次触发onConfigurationChanged(Configuration)


我已经设置了这个属性,但它不起作用。第一次在oncreate()中调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);时,会触发onConfigurationChanged事件,但后来当我旋转设备时,此事件不再触发,配置也没有被更改。 - Munazza
是的 :( android:configChanges="orientation|screenSize" 这就是我添加的内容。 - Munazza
我实际上想要在横屏模式下启动活动,并随后根据用户旋转设备来更改方向...有任何想法或解决方案,请发布? - Munazza
非常感谢,但您能告诉我如何实现我的要求吗?这很好,如果我只想处理运行时方向更改的活动,但我想在启动活动后通过代码将配置设置为横向,然后想在旋转设备时处理配置更改。当我调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)更改方向时出现问题,它将方向设置为横向,但是即使我在纵向上旋转设备,也无法返回到纵向模式。 - Munazza
我认为这是一种取巧的方法。在你的清单文件中,添加android:screenOrientation="portrait",然后在onCreate()中调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);顺便说一句,如果这解决了你的问题,请接受它。 :) - user948620
显示剩余2条评论

0

我不太清楚您的需求。如果您不介意重新启动活动,您可以跳过覆盖 onConfigurationChanged。系统会为您处理方向更改。如果您不希望在方向更改时重新启动它,请提到 <activity android:configChanges="keyboardHidden|orientation|screenSize"/>,并覆盖 onConfigurationChanged 并调用 setContentView()

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentview(R.layout.activity_hls);
    initializeViews();//here you can initialize all your memberVariables using findViewbyID()
}

如果我只想处理活动运行时的方向更改,那么这还可以。但是,一旦启动活动,我想通过代码将配置设置为横向,然后在旋转设备时处理配置更改。当我调用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)更改方向时,问题就出现了。这会将方向设置为横向,但即使我在纵向上旋转设备,也无法返回到纵向模式。 - Munazza

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