android layout landscape/portrait with different options

时间:2016-04-25 08:55:50

标签: android layout landscape-portrait

Is there any way to directly tell android which layout resource to choose for landscape or portrait mode?

I know normally it's simple, put the normal "main_layout.xml" in the "res/layout" folder and the landscape "main_layout.xml" in the "res/layout-land" folder.

Now assume I have two layouts called "main_layout_blue.xml" and "main_layout_red.xml". The user can choose one of them and I can set the appropriate layout. On orientation change again all is fine.

Where I get stuck is by giving the user an option to set different layouts for different orienations like "main_layout_blue.xml" for portrait mode and "main_layout_red.xml" for landscape mode.

Is there an android option to explicitly tell the system to use "res/layout/main_layout_blue.xml" for the portrait orientation and "res/layout-land/main_layout_red.xml" for the landscape orientation? What am I missing here?

1 个答案:

答案 0 :(得分:0)

After the orientation change, the view will be recreated so you can check the orientation of the device by the following:

Activity.getResources().getConfiguration().orientation

and based on the result you can:

   if(portrait)
      setContentView(R.layout.blue)
   else
      setContentView(R.layout.red)

but keep in mind to put all of the resources in /layout folder