首页 文章

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

提问于
浏览
129

我已经看到了能够为一个活动指定两个单独的布局xml文件的引用,一个用于Portrait,一个用于Landscape . 我没有找到任何有关如何做到这一点的信息 . 如何为每个活动指定哪个xml文件是纵向布局,哪个是横向布局?

是否也可以为不同的屏幕尺寸指定不同的布局?如果是这样,这是怎么做到的?

8 回答

  • 3

    创建一个 layout-land 目录,并将布局XML文件的横向版本放在该目录中 .

  • 2

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

    更多信息:

    http://developer.android.com/guide/practices/screens_support.html

    在“屏幕大小和密度的资源目录限定符”下

  • 1

    For Mouse lovers! 我说右键单击资源文件夹和 Add new resource file ,并从可用限定符中选择 orientation

    enter image description here


    但是你仍然可以通过添加子文件夹“layout-land”来手动完成

    “你的项目方向\程序\ SRC \主\ RES”

    从那时起,此子文件夹下的任何layout.xml文件仅适用于横向模式 automatically .

    使用“layout-port”作为纵向模式 .

  • 59

    只是提醒:

    如果您定义了清单 xml 文件中的活动,请从 android:configChanges 属性中删除 orientation

    android:configChanges="orientation|screenLayout|screenSize"
    
  • 190

    创建一个新目录 layout-land ,然后在 layout-land 中创建具有相同名称的 xml 文件,因为它是 layout 目录并将您的内容对齐为横向模式 .

    请注意, xml 中的内容ID相同 .

  • 19

    或者用这个:

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

    下面的最后一行是应用两个量词的示例: landscape and smallest width (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/

  • 0

    我认为最新Android版本中最简单的方法是转到XML的设计模式(而不是文本) .

    然后从菜单中选择选项 - 创建横向变化 . 这将在几秒钟内创建一个风景xml,没有任何麻烦 . 最新的Android Studio版本允许您立即创建横向视图 .

    enter image description here

    我希望这适合你 .

相关问题