我正在尝试让我的应用在方向更改时更改布局。这是我的代码:
package com.wish.list;
import com.wish.list.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.WindowManager;
import android.widget.ListView;
public class ListOfLists extends Activity{
public void onCreate(Bundle savedInstanceState) {
// Orientation Change starts here:
private void setContentBasedOnLayout()
WindowManager winMan = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
if (winMan != null)
{
int orientation = winMan.getDefaultDisplay().getOrientation();
if (orientation == 0) {
// Portrait
setContentView(R.layout.listsportrait);
}
else if (orientation == 1) {
// Landscape
setContentView(R.layout.listslandscape);
}
}
}
// End of Orientation Change
ListView listsList = (ListView) findViewById(R.id.lists);
}
}
}
在线
public void onCreate(Bundle savedInstanceState) {
我收到此错误:
“此行有多个标记:
- 语法错误,插入“}”以完成MethodBody
- 覆盖android.app.Activity.onCreate“
此外,有没有办法更改它,所以它不必重新启动方向更改活动?我有它所以Facebook在应用程序开始时检查用户的登录详细信息,并且每次方向更改时都必须重新检查它。
修改
所以代码现在看起来应该是这样的吗?
package com.wish.list;
import com.wish.list.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.OrientationEventListener;
import android.view.WindowManager;
import android.widget.ListView;
public class ListOfLists extends Activity{
public void onCreate(Bundle savedInstanceState) {
}
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.listslandscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.listsportrait);
}
}
// Orientation Change starts here:
private void setContentBasedOnLayout(){
WindowManager winMan = (WindowManager)getBaseContext().getSystemService(Context.WINDOW_SERVICE);
if (winMan != null)
{
int orientation = winMan.getDefaultDisplay().getOrientation();
if (orientation == 0) {
// Portrait
setContentView(R.layout.listsportrait);
}
else if (orientation == 1) {
// Landscape
setContentView(R.layout.listslandscape);
}
}
// End of Orientation Change
ListView listsList = (ListView) findViewById(R.id.lists);
}
}
或者我是否删除了setContentBasedOnLayout部分并将其替换为onConfigurationChanged代码?
答案 0 :(得分:4)
onCreate
后插入“}”如果您不希望在方向更改时重新启动活动,
1.清单下面的声明:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
2.取消活动中的onConfigurationChanged
。
@Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.listslandscape);
}
else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
setContentView(R.layout.listsportrait);
}
}
修改强>
是的,您应该删除setContentBasedOnLayout
部分。方向更改后,将调用onConfigurationChanged
而不是onCreate
。