2013-10-30 68 views
-1

我正在开发一个项目。在那里我使用屏幕方向风景和肖像。我还使用android:configChanges="keyboardHidden|orientation|screenSize",以便在切换方向时不刷新活动。Android的方向更改

现在应用程序无法从layout-port-> layout-land xml布局切换,因为我使用configChanges =“orientation”。

我能够交换机从Landscape->纵向或纵向状态>风景,但它表明从两个方向,而不是从布局端口 - >布局土地或
布局陆地>布局开关的布局端口的xml -港口。

+0

您sohuldn​​t使用android:configChanges =“keyboardHidden | orientation | screenSize”来处理方向更改。检出onSaveInstanceState和onRestoreInstanceState –

+0

让我们清楚。 'android:configChanges =“orientation”'是一种破解。它只能用在非常特殊的情况下。如果你搜索它为什么不好(非常糟糕),有很多讨论。总之,你掩盖了后来在其他场景中会出现的错误。正确的答案是根据这一点学习Activity生命周期和代码。这种攻击是懒惰的方式来掩盖他们的代码中仍然存在等待被发现的问题。 – Simon

回答

0

在乌尔活动

inside of oncreate() 
if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_LANDSCAPE) 
     setContentView(R.layout.land); 
else if(getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) 
     setContentView(R.layout.port); 



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


    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     setContentView(R.layout.land); 
    } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     setContentView(R.layout.port); 
    } 
} 
+0

我正在使用布局和布局 - 土地,但它显示布局xml在陆地和港口视图不切换到布局土地。 – user2775735

0

添加这个在AndroidManifest文件只添加android:configChanges="orientation"和删除其他两个用于活动要处理这个方向

0

试试这个

变化android:configChanges="keyboardHidden|orientation|screenSize"

android:configChanges="orientation"

和您的活动,覆盖onConfigurationChanged梅索德

像马尼万南在他的回应

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


    if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     //set your landscape layout 
    } else if (config.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     //the same for portrait layout 
    } 
} 

就是这样描述。

+0

谢谢...我跟着你回答...如果我只使用android:configChanges =“orientation”活动重新加载方向change..i不想重新加载活动方向更改 – user2775735

+0

比添加screenSize android:configChanges –

+0

if我添加screenSize然后它不起作用 – user2775735