Tablet or Phone - Android

后端 未结 30 2532
难免孤独
难免孤独 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:04

    No code needed

    The other answers list many ways of programmatically determining whether the device is a phone or tablet. However, if you read the documentation, that is not the recommended way to support various screen sizes.

    Instead, declare different resources for tablets or phones. You do this my adding additional resource folders for layout, values, etc.

    • For Android 3.2 (API level 13) on, add a sw600dp folder. This means the smallest width is at least 600dp, which is approximately the phone/tablet divide. However, you can also add other sizes as well. Check out this answer for an example of how to add an additional layout resource file.

    • If you are also supporting pre Android 3.2 devices, then you will need to add large or xlarge folders to support tablets. (Phones are generally small and normal.)

    Here is an image of what your resources might like after adding an extra xml files for different screen sizes.

    When using this method, the system determines everything for you. You don't have to worry about which device is being used at run time. You just provide the appropriate resources and let Android do all the work.

    Notes

    • You can use aliases to avoid duplicating identical resource files.

    Android docs worth reading

    • Supporting Multiple Screens
    • Supporting Different Screen Sizes
    • Distributing to Specific Screens

提交回复
热议问题