如何为纵向和横向指定不同的布局?
我见过关于能够为一个活动指定两个单独的布局 xml 文件的参考资料,一个用于纵向,一个用于横向。不过,我还没有找到任何有关如何做到这一点的信息。如何为每个活动指定哪个 xml 文件是纵向布局,哪个是横向布局?
是否也可以为不同的屏幕尺寸指定不同的布局?如果是这样,这是如何完成的?
I've seen references to being able to specify two separate layout xml files for an activity, one for Portrait and one for Landscape. I've not been to find any information on how to do that though. How do I specify for each activity which xml file is it's portrait layout and which is the Landscape layout?
Is it also possible to specify different layouts for different screen sizes? If so, how is this done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
创建
layout-land
目录并将布局 XML 文件的横向版本放入该目录中。Create a
layout-land
directory and put the landscape version of your layout XML file in that directory.您只需根据方向和分辨率将其放在具有不同名称的单独文件夹下,设备将自动为其屏幕设置选择正确的文件夹
更多信息:
http://developer.android.com/guide/practices/screens_support.html
位于“屏幕尺寸和密度的资源目录限定符”下
You just have to put it under separate folders with different names depending on orientation and resolution, the device will automatically select the right one for its screen settings
More info here:
http://developer.android.com/guide/practices/screens_support.html
under "Resource directory qualifiers for screen size and density"
对于鼠标爱好者!我说右键单击资源文件夹并
添加新资源文件
,然后从可用限定符中选择方向
:< /a>
但您仍然可以手动执行此操作,例如将子文件夹“layout-land”添加到
“Your-Project-Directory\app\src\main\res”,
此后此子文件夹下的任何layout.xml文件文件夹仅适用于横向模式自动。
使用“layout-port”进行纵向模式。
For Mouse lovers! I say right click on resources folder and
Add new resource file
, and from Available qualifiers select theorientation
:But still you can do it manually by say, adding the sub-folder "layout-land" to
"Your-Project-Directory\app\src\main\res"
since then any layout.xml file under this sub-folder will only work for landscape mode automatically.
Use "layout-port" for portrait mode.
Android Studio 3.xx 和 Android Studio 4.xx 的最快方法
1.转到活动布局的设计选项卡
2.在顶部,您应该按预览方向按钮,有一个用于创建横向布局的选项(检查图像),将创建一个新文件夹作为该特定方向的 xml 布局文件
更新: 在较新的版本中,选项已移至.xml 文件名按钮。 (非常感谢奥利弗霍夫曼指出了这一点)。
Fastest way for Android Studio 3.x.x and Android Studio 4.x.x
1.Go to the design tab of the activity layout
2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation
Update: On newer versions the options have been moved to the context menu of the .xml filename button. (Big thanks to Oliver Hoffmann for pointing that out).
只是提醒一下:
从清单
xml
文件中活动的android:configChanges
属性中删除orientation
(如果您已定义):Just a reminder:
Remove
orientation
fromandroid:configChanges
attribute for the activity in your manifestxml
file if you defined it:我认为最新 Android 版本中最简单的方法是进入 XML(而不是文本)的设计模式。
然后从菜单中选择选项 - 创建景观变化。
这将在几秒钟内轻松创建一个横向 xml。最新的 Android Studio 版本允许您立即创建横向视图。
我希望这对您有用。
I think the easiest way in the latest Android versions is by going to Design mode of an XML (not Text).
Then from the menu, select option - Create Landscape Variation.
This will create a landscape xml without any hassle in a few seconds. The latest Android Studio version allows you to create a landscape view right away.
I hope this works for you.
创建一个新目录
layout-land
,然后在layout-land
中创建与layout
同名的xml
文件> 目录并在其中对齐横向模式的内容。请注意,两个
xml
中的内容 id 是相同的。Create a new directory
layout-land
, then createxml
file with same name inlayout-land
as it waslayout
directory and align there your content for Landscape mode.Note that id of content in both
xml
is same.下面的最后一行是应用两个量词的示例:横向和最小宽度(600dp) 屏幕。使用您需要的更新 600dp。
上述内容也适用于尺寸
一个有用的设备指标:https://material.io/tools/devices/< /a>
The last line below is an example for applying two quantifiers: landscape and smallest width(600dp) screen. Update 600dp with the ones you need.
The above applies to dimens as well
A useful device metrics: https://material.io/tools/devices/
或者使用这个:
Or use this:
使用 Android Studio 3.4.1,它不再创建layout-land
文件夹。它将创建一个文件夹并将两个布局文件放在一起。Using Android Studio 3.4.1, it no longer createslayout-land
folder. It will create a folder and put two layout files together.在 Android Studio Chipmunk 中,您正在寻找的选项都在这里。
对于景观,选项是 XML 名称下拉列表中的“创建景观限定符”。
对于不同的屏幕尺寸,选项是“创建平板电脑限定符”,
这些选项已从以前的 Android Studio 版本中移出。
来源:https://stackoverflow.com/a/74047314/3718756
In Android Studio Chipmunk, the options you are looking for are here.
For landscape, the option is "Create Landscape Qualifier" in the XML name drop-down.
For different screen sizes, the option is "Create Tablet Qualifier"
these have been moved from previous Android Studio versions.
Source: https://stackoverflow.com/a/74047314/3718756