作者sorrel20567 (嗨)
看板AndroidDev
標題Re: [問題] 隨著直放橫放即時改變配置xml
時間Wed Oct 19 23:05:36 2011
我試了在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
推 milochen:感謝分享 :) 10/20 10:49
推 Horny5566:太棒了! 10/20 16:21
→ sorrel20567:補充,layout-port跟land裡面的檔名都要一樣 10/20 21:52
推 Wolfken:我記得這樣還是在某些機子上會有問題,因為後來的Android 10/25 13:00
→ Wolfken:改了API,沒有所謂portrait landscape的概念,只有相對於 10/25 13:00
→ Wolfken:它原生layout轉幾度的概念,這所謂原生layout,在手機跟 10/25 13:01
→ Wolfken:tablet會不一樣,所以你根據一個寫,在另一個會掛掉 10/25 13:01
→ sorrel20567:樓上正解,補充:最好判斷長寬,再去換LAYOUT會比較好 11/05 14:12