Android设备OnConfiguration更改事件不处理方向

Android设备OnConfiguration更改事件不处理方向,android,configuration,orientation,landscape,portrait,Android,Configuration,Orientation,Landscape,Portrait,我有一个活动,在启动该活动时,我需要在lanscape中更改方向,然后我想在用户旋转设备时处理两个方向的更改,但它一旦更改方向,以后不会更改方向。这是我的密码,请帮忙 public class Hls extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layo

我有一个活动,在启动该活动时,我需要在lanscape中更改方向,然后我想在用户旋转设备时处理两个方向的更改,但它一旦更改方向,以后不会更改方向。这是我的密码,请帮忙

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

}
根据这项声明

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

你打电话

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

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


删除它将再次触发配置更改(配置)

我不清楚您的要求。如果您不介意重新启动活动,您可以跳过覆盖
onConfigurationChanged
。系统将为您处理方向更改。如果您不希望在方向更改时重新启动,只需提及
,并覆盖
onConfigurationChanged
并调用
setContentView()


在onCreate中使用setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u横向)会将您的方向永久固定到横向,并避免方向发生任何更改。这就是为什么你的方向是固定的,对旋转没有反应

以下是您可以使用的备选方案:-

1> 在布局中创建两个视图。i、 一个用于景观,另一个用于纵向视图。比如说活动区和活动区

2> 在OnConfiguration Changed(Configuration newConfig)中使用设置内容视图(R.layout.activity\u hls),而不是设置请求方向(ActivityInfo.SCREEN\u ORIENTATION\u横向)或设置请求方向(ActivityInfo.SCREEN\u ORIENTATION\u纵向)

以下是示例代码:-

 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>

活动\u hls\u端口的示例布局:-

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


我已设置此属性,但不起作用。第一次设置RequestedOrientation时(ActivityInfo.SCREEN\u ORIENTATION\u横向);在oncreate()中调用,会触发onConfigurationChanged事件,但稍后当我旋转设备时,不会触发此事件,也不会更改配置。是:(android:configChanges=“orientation | screenSize”这就是我添加的内容。事实上,我想在横向模式下启动活动,然后在用户旋转设备时更改方向……有任何想法或解决方案,请发布?非常感谢,但请告诉我如何实现我的要求……如果我只想处理活动运行时方向的更改,这是可以的,但我nt在活动启动后通过代码将配置设置为横向,然后希望在旋转设备时处理配置更改。当我通过调用setRequestedOrientation(ActivityInfo.SCREEN\u orientation\u横向)更改方向时,会出现问题;这将方向设置为lanscape,但即使我以我认为的方式在肖像上旋转设备,我也无法返回肖像模式。在您的清单中,放置android:screenOrientation=“肖像”,然后在onCreate()中,调用setRequestedOrientation(ActivityInfo.SCREEN\u orientation\u SENSOR);顺便说一句,如果这解决了您的问题,请接受它。:)如果我只想处理活动运行时方向的更改,但我想在活动启动后通过代码将配置设置为横向,然后处理设备上的配置更改,那么这是可以的。当我通过调用setRequestedOrientation(ActivityInfo.SCREEN\u orientation\u横向)更改方向时,就会出现问题;这将方向设置为lanscape,但即使在纵向上旋转设备,我也无法返回纵向模式。好的,thnx plz告诉我如何设计横向布局,我的意思是b/w端口和陆地布局有什么不同..我的portlay是Hi!这没有任何区别。你需要做的就是创建横向布局。如何在布局文件中指定横向模式?这只是横向模式的外观。你需要在布局中像在横向模式下一样排列视图。我添加了示例布局。试试看。
<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>