简体   繁体   English

如何在横向布局中包含纵向布局?

[英]How to include a portrait layout in a landscape layout?

I have designed the perfect portrait layout.我设计了完美的纵向布局。 I'd like to include is as a part of the landscape version (see attachment).我想将 is 作为横向版本的一部分(见附件)。

横向版本的布局

There is no extra functionality, so I don't see a need to go to trouble of using Fragments.没有额外的功能,所以我认为没有必要在使用 Fragments 时遇到麻烦。

Of course, one cannot include the portrait version of a layout in the landscape version.当然,不能在横向版本中包含布局的纵向版本。 The following code produces the obvious error of "layouts should not include itself":以下代码产生“布局不应包含自身”的明显错误:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    >

    <include
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        layout="@layout/my_perfect_layout"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/demo"
        />
</LinearLayout>

Is there a way to force a layout directory so that I don't get this include infinite loop?有没有办法强制布局目录,这样我就不会包含无限循环? Or is there another way altogether?还是完全有另一种方式? I'd really like to avoid changing any java code at this point!在这一点上,我真的很想避免更改任何 Java 代码!

Best practice:最佳实践:

  • Put your content layout into a orientation-less layout file.将您的内容布局放入无方向的布局文件中。 (this is the reusable part) (这是可重复使用的部分)
  • Create a frame layout file for all orientations you wnat to use and include the content layout.为您想要使用的所有方向创建一个框架布局文件,并包含内容布局。 (this is the part where you put layout-specific parts, eg include more stuff in landscape) (这是您放置特定于布局的部分的部分,例如在横向中包含更多内容)

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

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