0

为了处理方向变化,我为我的活动使用了以下代码片段。

[Activity (Label = "Activity",ConfigurationChanges = ConfigChanges.Orientation 
| ConfigChanges.KeyboardHidden)]

public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            if (newConfig.Orientation == Android.Content.Res.Orientation.Landscape)
            {
                Console.WriteLine("landscape");
            } 
            else if (newConfig.Orientation == Android.Content.Res.Orientation.Portrait)
            {
                Console.WriteLine("portrait");
            }
        }

我从Portrait模式开始,然后切换到Landscape mode并再次切换回Portrait模式。所以预期的输出应该是:

landscape

portrait

控制台输出显示

landscape

landscape

portrait

即当从模式切换时,Landscape modeifelse被执行。Portrait

我不知道为什么会这样。

我是Mono for Android 的绝对初学者,所以任何帮助表示赞赏。

4

3 回答 3

1

这是我的代码

if (Resources.Configuration.Orientation == Android.Content.Res.Orientation.Landscape)
{
    SetContentView(Resource.Layout.Main); //set layout for Landscape mode
}
else
{
    SetContentView(Resource.Layout.MainPortrait); //set layout for Portrait mode
}
于 2012-09-21T02:38:00.920 回答
0

试试这个它的工作...

1)在上面声明oncreate()

int prev_orientation =0;

2)覆盖以下方法:

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        int orientation = getResources().getConfiguration().orientation;
        if(prev_orientation!=orientation){
            prev_orientation = orientation;
        if(orientation ==1){            
            //por
            Toast.makeText(getApplicationContext(),"ort::pot"+orientation,Toast.LENGTH_LONG).show();
        }else if(orientation ==2){
            //land
            Toast.makeText(getApplicationContext(),"ort::land"+orientation,Toast.LENGTH_LONG).show();
        }
        }
        super.onConfigurationChanged(newConfig);
    }

3)在活动清单文件中添加以下行:

android:configChanges="keyboardHidden|orientation"
于 2012-04-02T05:55:07.793 回答
0

我在这里粘贴代码..试试这个..

public void onConfigurationChanged(Configuration orient) 
    {
        super.onConfigurationChanged(orient);

        // Checks the orientation of the screen
        if (orient.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
                      //code here for LANDSCAPE..
        ));   

        }
        else if (orient.orientation == Configuration.ORIENTATION_PORTRAIT)
        {           
                      //code here for PORTRAIT ..   
            }               
    }

调用方法喜欢;

  @Override
public void onCreate(Bundle savedInstanceState) 
{
        onConfigurationChanged(getResources().getConfiguration());
    }
于 2012-04-02T06:00:15.093 回答