Tablet or Phone - Android

后端 未结 30 2175
难免孤独
难免孤独 2020-11-22 08:33

Is there a way to check if the user is using a tablet or a phone? I\'ve got problems with my tilt function and my new tablet (Transformer)

30条回答
  •  难免孤独
    2020-11-22 09:20

    As it has been mentioned before, you do not want to check whether the device is a tablet or a phone but you want to know about the features of the device,

    Most of the time, the difference between a tablet and a phone is the screen size which is why you want to use different layout files. These files are stored in the res/layout- directories. You can create an XML file in the directoy res/values- for each of your layouts and put an int/bool/string resource into it to distinguish between the layouts you use.

    Example:

    File res/values/screen.xml (assuming res/layout/ contains your layout files for handsets)

    
    
      phone
    
    


    File res/values-sw600dp/screen.xml (assuming res/layout-sw600dp/ contains your layout files for small tablets like the Nexus 7)

    
    
      7-inch-tablet
    
    


    File res/values-sw720dp/screen.xml (assuming res/layout-sw720dp/ contains your layout files for large tablets like the Nexus 10):

    
    
      10-inch-tablet
    
    


    Now the screen type is accessible via the R.string.screen_type constant.

提交回复
热议问题