我有一个活动,在活动启动时我需要在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);
}
}
答案 0 :(得分:6)
在onCreate中使用setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)会将您的方向永久固定为横向,并避免方向发生任何变化。这就是为什么你的方向得到修复而不响应轮换的原因。
您可以使用以下选项: -
1>在布局中创建两个视图。即一个用于横向,一个用于纵向视图。让我们说activity_hls_land和activity_hls_port。
2 - ;在onConfigurationChanged(配置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>
答案 1 :(得分: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)
答案 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()
}