How can I use 2 different background between landscape mode and portrait mode

后端 未结 2 952
北恋
北恋 2021-01-12 08:23

I have an android application, I would like to know if I can have 1 layout (1 layout xml file) for both landscape and portrait mode. But I want a different background for ea

2条回答
  •  爱一瞬间的悲伤
    2021-01-12 09:03

    If you're willing to use code to accomplish this, you might try inserting something like

    View view = (View) findViewById(R.id.background_carrying_view);
    int orientation = getResources().getConfiguration().orientation;
    if(orientation == Configuration.ORIENTATION_LANDSCAPE) {
        view.setBackgroundResource (R.drawable.background_land);
    } else {
        view.setBackgroundResource (R.drawable.background_port);
    }
    

    into your onCreate() and/or onConfigurationChange().

    With this method, you would use the same layout for both landscape and portrait.

提交回复
热议问题