Android Device onConfigurationChanged event does not handling orientation
我有一个活动,在活动启动时,我需要更改 lanscape 中的方向,然后我想在用户旋转设备时处理两个方向更改,但它一旦更改方向,以后不会更改方向。这是我的代码,请帮助
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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); } } |
在 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)
这里是示例代码:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 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" 在活动中:-
1 2 3 4 5 6 7 8 9 10 11 12 | <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:configChanges="orientation" > <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> |
activity_hls_port 的示例布局:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <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> |
横向模式示例:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <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> |
关于此声明
1 2 3 4 5 6 7 8 9 10 | 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); } |
也在
中
你打电话给
1 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
或
1 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
这意味着您的活动处于横向/纵向方向并且不再旋转
删除它会再次触发
我不清楚您的要求。如果你不介意重新开始你的活动,你可以跳过覆盖
1 2 3 4 5 | public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); setContentview(R.layout.activity_hls); initializeViews();//here you can initialize all your memberVariables using findViewbyID() } |