How to programatically determine which XML layout

2020-05-24 04:40发布

How do I programatically determine which layout (layout-normal, layout-large, etc.) my app is currently using? I see the getWindowManager().getDefaultDisplay().getMetrics(metrics) call but it seems to only deal with screen density but not necessary which layout the app is using. I need the info since I need to programatically change some of the positioning of views dynamically at runtime.

Thanks in advance.

6条回答
放荡不羁爱自由
2楼-- · 2020-05-24 04:49

I was searching for the same thing! I found 2 similar questons/answers:

Getting ScreenLayout

Using ScreenLayout Bitmask

While all three questions are essentially the same, you really need both answers to get your result.

I used this code to get the layout size inside my Activity:

int layoutSize = getResources().getConfiguration().screenLayout;
layoutSize = layoutSize & Configuration.SCREENLAYOUT_SIZE_MASK;

The first line will return the layout size, but it will be a number that is almost the maximum size of an int.

The second line will AND the layout size with the given mask. The mask has a value of 15 in decimal, or F in hex.

The value returned is between 0 and 3. The values corresponding to screen sizes, as well as the value of the size mask, are defined as constants in the android.content.res.Configuration class: Android Configruation Documentation.

All you need to do after the previous 2 lines is to have some kind of a switch statement comparing the returned layoutSize with Configuration's pre-defined values for SCREENLAYOUT_SIZE_XXX.

Hope this is not too late for you to use it. My problem was that I did not know how to ask the question in exact Android language. That is probably why the same question was, and probably should be, asked so many different ways.

查看更多
时光不老,我们不散
3楼-- · 2020-05-24 04:50

That's an old question, but I would like to share my way of solving this issue.

Create a values-* variant matching each setup you wish to confirm programmatically, inside which you'll define the proper values describing the current configuration:

enter image description here

As you can see, the boolean value is_landscape exists both in values and values-land, therefore it is possible to safely access it via R.bool.is_landscape.

The only difference, though, is the value:

getResources().getBoolean(R.bool.is_landscape); evaluates to true in landscape mode, and false otherwise.

Note: of course that it is possible to use getResources().getConfiguration().orientation in order to get the current device orientation, though I chose to use this simple familiar case to make my example clear and straight forward.

It is important to understand that this technique is versatile, using which one can define a more complex values resource directory, ( e.g. values-ldrtl-sw600dp-v23 ) to support a more specific configuration.

查看更多
何必那么认真
4楼-- · 2020-05-24 04:56

I had the same problem. If you're using layout/layout-sw600dp/layout-sw720dp, the following ended working for me:

Configuration config = activity.getResources().getConfiguration();

if (config.smallestScreenWidthDp >= 720) {
  // sw720dp code goes here
}
else if (config.smallestScreenWidthDp >= 600) {
  // sw600dp code goes here
}
else {
  // fall-back code goes here
}
查看更多
该账号已被封号
5楼-- · 2020-05-24 04:56
ViewGroup view = (ViewGroup)getWindow().getDecorView();
LinearLayout content = (LinearLayout)view.getChildAt(0);
int id = content.getId();

The id tells you which layout you're using (assuming you set the id tag in the XML).

查看更多
Bombasti
6楼-- · 2020-05-24 05:04

An extremely easy way to do this, is by setting a id and tag to the parent layout and in your onCreate(), you can findViewById() and getTag().

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/my_activity_view"
    android:tag="big_screen" >
        // misc views~
</RelativeLayout>

And then, in your onCreate method,

if(findViewById(R.id.my_activity_view).getTag().equals("big_screen")) {
    // do stuff
}
查看更多
淡お忘
7楼-- · 2020-05-24 05:04

Following code will help you. I just printed corresponding screen size or density category. U can do whatever you want!

//Determine screen size
    if ((getResources().getConfiguration().screenLayout &Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) 
    {     
        Log.d("Screen Size: ", "LARGE");

    }
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {     
        Log.d("Screen Size: ", "NORMAL");

    } 
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {     
        Log.d("Screen Size: ", "SMALL");
    }
    else if ((getResources().getConfiguration().screenLayout &      Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {     
        Log.d("Screen Size: ", "XLARGE");
    }
    else {
        Log.d("Screen Size: ","UNKNOWN_CATEGORY_SCREEN_SIZE");
    }




    //Determine density
    DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int density = metrics.densityDpi;

        if (density==DisplayMetrics.DENSITY_HIGH) {
            Log.d("Screen Density: ","HIGH");
        }
        else if (density==DisplayMetrics.DENSITY_MEDIUM) {
            Log.d("Screen Density: ","MEDIUM");
        }
        else if (density==DisplayMetrics.DENSITY_LOW) {
            Log.d("Screen Density: ","LOW");
        }
        else if (density==DisplayMetrics.DENSITY_XHIGH) {
            Log.d("Screen Density: ","XHIGH");
        }
        else if (density==DisplayMetrics.DENSITY_XXHIGH) {
            Log.d("Screen Density: ","XXHIGH");
        }
        else {
            Log.d("Screen Density: ","UNKNOWN_CATEGORY");
        }
查看更多
登录 后发表回答