2010-01-23 96 views
115

我见过可以为活动指定两个单独的布局xml文件的引用,一个用于纵向,一个用于横向。我还没有找到任何有关如何做到这一点的信息。如何为每个活动指定哪个xml文件是纵向布局,哪个是横向布局?如何为纵向和横向定义不同的布局?

是否也可以为不同的屏幕尺寸指定不同的布局?如果是这样,这是如何完成的?

回答

169

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

+2

这是否意味着如果我这样做,所有活动都需要定义横向和纵向视图? – 2010-01-23 19:01:52

+13

不,如果不存在布局定义,它只是使用标准布局目录中的XML布局。 – 2010-01-23 19:49:16

+0

在我的情况下,layout-land不工作,当我使用,android:configChanges =“orientation | keyboardHidden | screenSize” – 2014-02-11 06:53:33

58

你只要把它放在与根据方向和分辨率不同的名称单独的文件夹,设备会自动选择合适的一个用于其屏幕设置

此处了解详情:

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

“屏幕尺寸和密度的资源目录限定词”

+0

仅适用于操作系统版本1.6或更高版本。如果你想支持1.5,你需要这个链接:http://developer.android.com/guide/practices/screens-support-1.5.html – 2011-09-19 14:13:52

+2

@Silvio Donnini - 链接不再具有所需的信息。 – itsaboutcode 2012-09-10 02:05:35

+2

@itsaboutcode是的,请执行以下操作:转至“使用配置限定符” – Danation 2012-11-02 18:11:35

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

提醒您:

android:configChanges属性,在清单xml文件中删除orientation的活动,如果你定义它:

android:configChanges="orientation|screenLayout|screenSize" 
3

创建一个新的目录layout-land,然后创建xml文件同名在layout-land,因为它是layout目录并在那里对齐您的内容的横向模式。

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

14

对于鼠标爱好者!我说右键单击资源文件夹,并添加新的资源文件,并从可用预选赛选择方向:

enter image description here


但你仍然可以说做手工,加上子文件夹“布局土地”

‘你的项目方向\程序\ SRC \主\ RES’

从那时起这个子文件夹下的任何layout.xml文件仅在横向模式下工作AUT omatically

在纵向模式下使用“Layout-port”。

相关问题