Android: alternate layout xml for landscape mode

后端 未结 6 506
故里飘歌
故里飘歌 2020-11-22 08:54

How can I have one layout for landscape and one for portrait? I want to assume extra width and conserve vertical space when the user rotates the phone over sideways.

相关标签:
6条回答
  • 2020-11-22 09:17

    Fastest way for Android Studio 3.x.x and Android Studio 4.x.x

    1.Go to the design tab of the activity layout

    2.At the top you should press on the orientation for preview button, there is a option to create a landscape layout (check image), a new folder will be created as your xml layout file for that particular orientation

    0 讨论(0)
  • 2020-11-22 09:18

    In the current version of Android Studio (v1.0.2) you can simply add a landscape layout by clicking on the button in the visual editor shown in the screenshot below. Select "Create Landscape Variation"

    Android Studio add landscape layout

    0 讨论(0)
  • 2020-11-22 09:18

    The layouts in /res/layout are applied to both portrait and landscape, unless you specify otherwise. Let’s assume we have /res/layout/home.xml for our homepage and we want it to look differently in the 2 layout types.

    1. create folder /res/layout-land (here you will keep your landscape adjusted layouts)
    2. copy home.xml there
    3. make necessary changes to it

    Source

    0 讨论(0)
  • 2020-11-22 09:25

    You can group your specific layout under the correct folder structure as follows.

    layout-land-target_version

    ie

    layout-land-19 // target KitKat

    likewise you can create your layouts.

    Hope this will help you

    0 讨论(0)
  • 2020-11-22 09:26

    I will try to explain it shortly.

    First, you may notice that now you should use ConstraintLayout as requested by google (see androix library).

    In your android studio projet, you can provide screen-specific layouts by creating additional res/layout/ directories. One for each screen configuration that requires a different layout.

    This means you have to use the directory qualifier in both cases :

    • Android device support
    • Android landscape or portrait mode

    As a result, here is an exemple :

    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
    

    You can also use qualifier with res ressources files using dimens.xml.

    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/dimens.xml

    <resources>
        <dimen name="grid_view_item_height">70dp</dimen>
    </resources>
    

    res/values-land/dimens.xml

    <resources>
        <dimen name="grid_view_item_height">150dp</dimen>
    </resources>
    

    your_item_grid_or_list_layout.xml

    <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintlayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content
    
        <ImageView
                android:id="@+id/image"
                android:layout_width="0dp"
                android:layout_height="@dimen/grid_view_item_height"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:background="@drawable/border"
                android:src="@drawable/ic_menu_slideshow">
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    Source : https://developer.android.com/training/multiscreen/screensizes

    0 讨论(0)
  • 2020-11-22 09:29

    By default, the layouts in /res/layout are applied to both portrait and landscape.

    If you have for example

    /res/layout/main.xml
    

    you can add a new folder /res/layout-land, copy main.xml into it and make the needed adjustments.

    orientation

    See also http://www.androidpeople.com/android-portrait-amp-landscape-differeent-layouts and http://www.devx.com/wireless/Article/40792/1954 for some more options.

    0 讨论(0)
提交回复
热议问题