How can I detect which layout is selected by Android in my application?

前端 未结 8 1973
孤街浪徒
孤街浪徒 2020-12-12 10:35

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

layout-land/my_act.xml
lay

相关标签:
8条回答
  • 2020-12-12 11:35

    You can get info about screen orientation and size from Resources object. From there you can understand which layout is used.

    getResources().getConfiguration().orientation; - returns either Configuration.ORIENTATION_PORTRAIT or Configuration.ORIENTATION_LANDSCAPE.

    int size = getResources().getConfiguration().screenLayout; - returns mask of screen size. You can test against Small, Normal, Large, xLarge sizes. For example:

    if ((size & Configuration.SCREENLAYOUT_SIZE_XLARGE)==Configuration.SCREENLAYOUT_SIZE_XLARGE)
    
    0 讨论(0)
  • 2020-12-12 11:38

    You can set a different android:tag attribute on the views in each different resource file, and read the tag back at runtime with View.getTag().

    Example:

    layout-xlarge-land/my_act.xml

    <View
        android:id="@+id/mainview"
        android:tag="xlarge-landscape"
    />
    

    layout-xlarge/my_act.xml

    <View
        android:id="@+id/mainview"
        android:tag="xlarge-portrait"
    />
    

    MyActivity.java

    String tag = view.getTag();
    if (tag.equals("xlarge-landscape") {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题