Re: [問題] 隨著直放橫放即時改變配置xml
我試了在onResume裡加上一個判斷直橫的方法
protected void onResume() {
super.onResume();
Log.v(TAG,"resumeeeeeeeeee");
setOri();
Log.v(TAG,"ori after");
if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
setContentView(R.layout.pagelayout_land);
Log.v(TAG2,"1");
}
else if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
setContentView(R.layout.pagelayout);
Log.v(TAG2,"0");
}
Log.v(TAG,"onResume");
}
看了DDMS的LOG後發現它無法進到if的判斷式裡,所以無法更換LAYOUT
我有試著把判斷直橫寫成另外一個方法setOri,onResume呼叫他
public void setOri()
{
Log.v(TAG,"oriiiiing");
int Ori = getRequestedOrientation();
if(Ori==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
Log.v(TAG,"LANDSCAPE");
}
else if(Ori==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
Log.v(TAG,"PORTRAIT");
}
}
發現它會進到oriiiiing 但不會進到裡面的判斷式
後來我找到了一個網站
http://www.apkcode.com/html/2011/interface_0218/280.html
它說在onCreate裡加上
if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
Log.i("info", "landscape");
}
else if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT) {
Log.i("info", "portrait");
}
前提是要在res目錄下建立layout-land和layout-port目錄
即可判斷
但還是無法
最後我找到了一個網站的方法可以用
http://disanji.net/2011/03/18/android-change-screen-activity/
首先是在manifest裡你想要橫直通吃的ACTIVITY上加上
android:configChanges="orientation"
接著在res目錄下建立layout-land和layout-port目錄
再來是在java檔裡加上
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//橫向
if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.pagelayout);
findview();
}
//直向
else if (this.getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.pagelayout);
findview();
}
}
就成功了
不過設完view後
必需要重新findview以及其他初始工作...
以上純分享
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.115.158.190
推
10/20 10:49, , 1F
10/20 10:49, 1F
推
10/20 16:21, , 2F
10/20 16:21, 2F
→
10/20 21:52, , 3F
10/20 21:52, 3F
推
10/25 13:00, , 4F
10/25 13:00, 4F
→
10/25 13:00, , 5F
10/25 13:00, 5F
→
10/25 13:01, , 6F
10/25 13:01, 6F
→
10/25 13:01, , 7F
10/25 13:01, 7F
→
11/05 14:12, , 8F
11/05 14:12, 8F
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):