Android: alternate layout xml for landscape mode

后端 未结 6 612
故里飘歌
故里飘歌 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: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

    
        70dp
    
    

    res/values-land/dimens.xml

    
        150dp
    
    

    your_item_grid_or_list_layout.xml

    
    
    
    

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

提交回复
热议问题