How can I detect which layout is selected by Andro

2019-01-20 23:17发布

Assume I have an activity with three different layouts in different resource folders. For example:

layout-land/my_act.xml
layout-xlarge/my_act.xml
layout-xlarge-land/my_act.xml

In different devices and different positions one of them is selected by Android.
How can I find out which one is selected programmatically?

Does Android have any API that returns these layouts to the program?


Edit: Graham Borland's solution has a problem in some situations that I mentioned in the comments.

8条回答
不美不萌又怎样
2楼-- · 2019-01-20 23:41

You could create a values-<config> directory for each of your supported configurations. Inside of each of those directories, create a strings.xml with a single selected_configuration string which describes the current configuration. At runtime, fetch the string using the standard getString method, which will do the configuration resolution for you and return the correct string for the configuration. This is untested.

查看更多
欢心
3楼-- · 2019-01-20 23:42

You can try to repeat this algorithm "How Android Finds the Best-matching Resource" - it's quite simple, especially if you have different layouts only for different screens.

查看更多
登录 后发表回答