How do I specify different layouts for portrait an

2019-01-01 10:44发布

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?

6条回答
牵手、夕阳
2楼-- · 2019-01-01 10:55

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"

查看更多
长期被迫恋爱
3楼-- · 2019-01-01 10:55

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-Direction\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.

查看更多
柔情千种
4楼-- · 2019-01-01 10:55

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"
查看更多
牵手、夕阳
5楼-- · 2019-01-01 10:57

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.

查看更多
高级女魔头
6楼-- · 2019-01-01 10:58

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>
查看更多
临风纵饮
7楼-- · 2019-01-01 11:03

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

查看更多
登录 后发表回答