如何为纵向和横向指定不同的布局?

发布于 2024-08-19 03:33:49 字数 155 浏览 14 评论 0原文

我见过关于能够为一个活动指定两个单独的布局 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

不羁少年 2024-08-26 03:33:49

创建 layout-land 目录并将布局 XML 文件的横向版本放入该目录中。

Create a layout-land directory and put the landscape version of your layout XML file in that directory.

梦在深巷 2024-08-26 03:33:49

您只需根据方向和分辨率将其放在具有不同名称的单独文件夹下,设备将自动为其屏幕设置选择正确的文件夹

更多信息:

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"

怪我鬧 2024-08-26 03:33:49

对于鼠标爱好者!我说右键单击资源文件夹并添加新资源文件,然后从可用限定符中选择方向

在此处输入图像描述< /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 the orientation :

enter image description here


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.

四叶草在未来唯美盛开 2024-08-26 03:33:49

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

enter image description here

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).

enter image description here

帅哥哥的热头脑 2024-08-26 03:33:49

只是提醒一下:

从清单 xml 文件中活动的 android:configChanges 属性中删除 orientation(如果您已定义):

android:configChanges="orientation|screenLayout|screenSize"

Just a reminder:

Remove orientation from android:configChanges attribute for the activity in your manifest xml file if you defined it:

android:configChanges="orientation|screenLayout|screenSize"
桃扇骨 2024-08-26 03:33:49

我认为最新 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.

enter image description here

I hope this works for you.

我是男神闪亮亮 2024-08-26 03:33:49

创建一个新目录layout-land,然后在layout-land中创建与layout同名的xml文件> 目录并在其中对齐横向模式的内容。

请注意,两个 xml 中的内容 id 是相同的。

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.

Note that id of content in both xml is same.

寒冷纷飞旳雪 2024-08-26 03:33:49

下面的最后一行是应用两个量词的示例:横向和最小宽度(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

上述内容也适用于尺寸

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

一个有用的设备指标: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.

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/

冷了相思 2024-08-26 03:33:49

或者使用这个:

<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>

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>
千年*琉璃梦 2024-08-26 03:33:49
  1. 右键res文件夹,
  2. 新建-> 可用限定符中的Android 资源文件
  3. ,选择方向,
  4. 添加到
  5. 屏幕方向中选择的限定符,选择横向
  6. 按 OK

使用 Android Studio 3.4.1,它不再创建 layout-land 文件夹。它将创建一个文件夹并将两个布局文件放在一起。

在此处输入图像描述

  1. Right click res folder,
  2. New -> Android Resource File
  3. in Available qualifiers, select Orientation,
  4. add to Chosen qualifier
  5. in Screen orientation, select Landscape
  6. Press OK

Using Android Studio 3.4.1, it no longer creates layout-land folder. It will create a folder and put two layout files together.

enter image description here

子栖 2024-08-26 03:33:49

在 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.

enter image description here

Source: https://stackoverflow.com/a/74047314/3718756

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文