Android 如何为纵向和横向指定不同的布局?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2124046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I specify different layouts for portrait and landscape orientations?
提问by Jay Askren
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?
我已经看到能够为活动指定两个单独的布局 xml 文件的参考资料,一个用于纵向,一个用于横向。我还没有找到任何关于如何做到这一点的信息。如何为每个活动指定哪个 xml 文件是纵向布局,哪个是横向布局?
Is it also possible to specify different layouts for different screen sizes? If so, how is this done?
是否也可以为不同的屏幕尺寸指定不同的布局?如果是这样,这是如何完成的?
回答by Mark B
Create a layout-land
directory and put the landscape version of your layout XML file in that directory.
创建一个layout-land
目录并将布局 XML 文件的横向版本放在该目录中。
回答by Silvio Donnini
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
http://developer.android.com/guide/practices/screens_support.html
under "Resource directory qualifiers for screen size and density"
在“屏幕大小和密度的资源目录限定符”下
回答by AmiNadimi
For Mouse lovers!I say right click on resources folder and Add new resource file
, and from Available qualifiers select the orientation
:
鼠标爱好者!我说右键单击资源文件夹和Add new resource file
,然后从可用限定符中选择orientation
:
But still you can do it manually by say, adding the sub-folder "layout-land" to
但是您仍然可以通过说手动完成,将子文件夹“layout-land”添加到
"Your-Project-Directory\app\src\main\res"
“你的项目目录\app\src\main\res”
since then any layout.xml file under this sub-folder will only work for landscape mode automatically.
此后,此子文件夹下的任何 layout.xml 文件将仅自动用于横向模式。
Use "layout-port" for portrait mode.
使用“layout-port”进行纵向模式。
回答by Robin Qiu
Just a reminder:
只是提醒:
Remove orientation
from android:configChanges
attribute for the activity in your manifest xml
file if you defined it:
如果您定义了它,orientation
则从android:configChanges
清单xml
文件中的活动的属性中删除:
android:configChanges="orientation|screenLayout|screenSize"
回答by Shid
Fastest way for Android Studio 3.x.x and Android Studio 4.x.x
Android Studio 3.xx 和 Android Studio 4.xx 的最快方法
1.Go to the design tab of the activity layout
1.转到活动布局的设计选项卡
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
2.在顶部,您应该按下预览按钮的方向,有一个选项可以创建横向布局(检查图像),将创建一个新文件夹作为该特定方向的 xml 布局文件
回答by Nouman Shah
Create a new directory layout-land
, then create xml
file with same name in layout-land
as it was layout
directory and align there your content for Landscape mode.
创建一个新的目录layout-land
,然后创建xml
的同名文件 layout-land
,因为它是layout
目录和调整你有景观模式的内容。
Note that id of content in both xml
is same.
请注意,两者中的内容 idxml
是相同的。
回答by Akanshi Srivastava
I think the easiest way in the latest Android versions is by going to Design mode of an XML (not Text).
我认为在最新的 Android 版本中最简单的方法是进入 XML(而不是文本)的设计模式。
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.
然后从菜单中选择选项 - 创建景观变化。这将在几秒钟内轻松创建景观 xml。最新的 Android Studio 版本允许您立即创建横向视图。
I hope this works for you.
我希望这对你有用。
回答by s-hunter
The last line below is an example for applying two quantifiers: landscape and smallest width(600dp) screen. Update 600dp with the ones you need.
下面的最后一行是应用两个量词的示例:横向和最小宽度(600dp) 屏幕。使用您需要的更新 600dp。
res/layout/main_activity.xml # For handsets
res/layout-land/main_activity.xml # For handsets in landscape
res/layout-sw600dp/main_activity.xml # For 7” tablets
res/layout-sw600dp-land/main_activity.xml # For 7” tablets in landscape
The above applies to dimens as well
以上也适用于尺寸
res/values/dimens.xml # For handsets
res/values-land/dimens.xml # For handsets in landscape
res/values-sw600dp/dimens.xml # For 7” tablets
res/values-sw600dp-land/dimens.xml # For 7” tablets in landscape
A useful device metrics: https://material.io/tools/devices/
一个有用的设备指标:https: //material.io/tools/devices/
回答by Giant
Or use this:
或者使用这个:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Add your UI elements inside the inner most linear layout -->
</LinearLayout>
</ScrollView>
回答by eos1d3
- Right click res folder,
- New -> Android Resource File
- in Available qualifiers, select Orientation,
- add to Chosen qualifier
- in Screen orientation, select Landscape
- Press OK
- 右键点击res文件夹,
- 新建 -> Android 资源文件
- 在可用限定符中,选择方向,
- 添加到选择的限定符
- 在屏幕方向中,选择横向
- 按确定
Using Android Studio 3.4.1, it no longer creates layout-land
folder. It will create a folder and put two layout files together.
使用 Android Studio 3.4.1,它不再创建layout-land
文件夹。它将创建一个文件夹并将两个布局文件放在一起。