简体   繁体   English

Android布局横向/纵向具有不同的选项

[英]android layout landscape/portrait with different options

Is there any way to directly tell android which layout resource to choose for landscape or portrait mode? 有什么方法可以直接告诉android哪种布局资源为横向或纵向模式选择?

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. 我知道通常很简单,将普通的“ main_layout.xml”放在“ res / layout”文件夹中,将横向的“ main_layout.xml”放在“ res / layout-land”文件夹中。

Now assume I have two layouts called "main_layout_blue.xml" and "main_layout_red.xml". 现在假设我有两个布局,分别是“ main_layout_blue.xml”和“ 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. 我遇到的麻烦是为用户提供了一个选项,可以为不同的方向设置不同的布局,例如纵向模式为“ main_layout_blue.xml”,横向模式为“ main_layout_red.xml”。

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? 是否有一个android选项明确指示系统将“ res / layout / main_layout_blue.xml”用于纵向,将“ res / layout-land / main_layout_red.xml”用于横向? What am I missing here? 我在这里想念什么?

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 但请记住将所有资源放在/ layout文件夹中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM