我有一个 Activity ,在 Activity 启动时我需要改变横向方向,然后我想在用户旋转设备时处理两个方向变化,但它一旦改变方向,以后就不会改变方向。这是我的代码,请帮忙
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> 在布局中创建两个 View 。即一个用于横向 View ,一个用于纵向 View 。让我们说 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 );
}
}
}
并在 list 中添加 android:configChanges="orientation"in activity :-
<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>
关于Android 设备 onConfigurationChanged 事件不处理方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13411391/