0
我試圖讓我的應用程序在方向更改時更改佈局。這是我的代碼:當方向更改錯誤時更改佈局
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代碼?
帶問題/新代碼的編輯OP – Cole 2012-03-26 03:46:10
@Cole你看到我的回答? – dreamtale 2012-03-26 12:30:27