我见过能够为一个活动指定两个单独的布局xml文件的引用,一个用于Portrait,一个用于Landscape。我没有找到任何有关如何做到这一点的信息。如何为每个活动指定哪个xml文件是纵向布局,哪个是横向布局?
是否也可以为不同的屏幕尺寸指定不同的布局?如果是这样,这是怎么做到的?
答案 0 :(得分:192)
创建 layout-land
目录,并将布局XML文件的横向版本放在该目录中。
答案 1 :(得分:59)
您只需根据方向和分辨率将其放在具有不同名称的单独文件夹下,设备将自动为其屏幕设置选择正确的文件夹
更多信息:
http://developer.android.com/guide/practices/screens_support.html
“屏幕大小和密度的资源目录限定符”
答案 2 :(得分:22)
对于鼠标爱好者!我说右键单击资源文件夹和Add new resource file
,然后从可用限定符中选择orientation
:
但是你仍然可以通过添加子文件夹" layout-land"手动完成。到
"你的项目方向\应用\ SRC \主\ RES"
此后,此子文件夹下的任何layout.xml文件仅适用于横向模式自动。
使用" layout-port"用于肖像模式。
答案 3 :(得分:19)
提醒一下:
如果您定义了清单orientation
文件中的活动,请从android:configChanges
属性中删除xml
:
android:configChanges="orientation|screenLayout|screenSize"
答案 4 :(得分:3)
创建一个新目录layout-land
,然后在xml
中创建与layout-land
目录同名的layout
文件,并将您的内容与横向模式对齐。
请注意,xml
中的内容ID都相同。
答案 5 :(得分:2)
或者使用它:
<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>
答案 6 :(得分:1)
我认为最新的Android版本中最简单的方法是进入XML(而非文本)的设计模式。
然后从菜单中选择选项-创建景观变化。 这将在几秒钟内轻松创建一个景观xml。最新的Android Studio版本允许您立即创建横向视图。
我希望这对您有用。
答案 7 :(得分:1)
下面的最后一行是应用两个量词的示例:风景和最小宽度(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
答案 8 :(得分:0)
使用Android Studio 3.4.1,它不再创建 layout-land
文件夹。它将创建一个文件夹并将两个布局文件放在一起。
答案 9 :(得分:0)