layout-land xml files does not work with onConfigurationChanged call back

后端 未结 2 1881
一生所求
一生所求 2021-01-21 09:36

I have different layouts for portrait and landscape mode and I also need to override the onConfigurationChanged() callback. But problem is when I change the phone

2条回答
  •  攒了一身酷
    2021-01-21 10:32

    I am sure that it will definitively help you...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int ot = getResources().getConfiguration().orientation;
        switch (ot) {
        case Configuration.ORIENTATION_LANDSCAPE:
            setContentView(R.layout.main_land);
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            setContentView(R.layout.main);
            break;
        }
        Toast.makeText(this, "Helloo", Toast.LENGTH_SHORT).show();
    }
    enter code here
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
    
        int ot = getResources().getConfiguration().orientation;
        switch (ot) {
        case Configuration.ORIENTATION_LANDSCAPE:
            setContentView(R.layout.main_land);
            break;
        case Configuration.ORIENTATION_PORTRAIT:
            setContentView(R.layout.main);
            break;
        }
    }
    
    @Override
    public Object onRetainNonConfigurationInstance() {
        // TODO Auto-generated method stub
        return super.onRetainNonConfigurationInstance();
    }
    

    }

    and add this line in your manifest file.. android:configChanges="keyboardHidden|orientation"

提交回复
热议问题