سؤال

I have a problem which I`m unable to solve lately..

I have make a simple app for tablet which I run in my Samsung Galaxy Note 8.0 .. I have set the layout to place some text view at some place.. I code it in xml using dp for margin and sp for text size. The width and height layout is set to wrapcontent. But when I run it in my friend Samsung Galaxy Tab 2 7.0, the layout did not match what I see in my device. The same thing happen when I try to run it in 10inch emulator.

I have use layout-sw600dp for my layout folder..

Why my screen layout is like that?I though by using layout-sw600dp the layout supposedly to adjust itself.. Or am I wrong regarding that?

I have read android documentation regarding different screensize support and so far I found using layout-sw600dp is good solution..

I have checked the device dpi using :

Display display = getWindowManager().getDefaultDisplay();
           DisplayMetrics outMetrics = new DisplayMetrics ();
            display.getMetrics(outMetrics);

            float density  = getResources().getDisplayMetrics().density;
            float dpHeight = outMetrics.heightPixels / density;
            float dpWidth  = outMetrics.widthPixels / density;
هل كانت مفيدة؟

المحلول

Every device has different pixel ratio, different dpi, different screen size and so on. Go to this link, read it, you will know how to manage different device layouts.

Android - Supporting Multiple Screen

نصائح أخرى

If you want to check the screen inch size when the app start, you can calculate the inch with the following two methods, but I think the second one is more precisely.

public static float getScreenInchSizeByDensityDpi(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    double x = Math.pow((double)dm.widthPixels / (double)dm.densityDpi, 2);
    double y = Math.pow((double)dm.heightPixels / (double)dm.densityDpi, 2);
    double screenInches = Math.sqrt(x + y);

    return (float) screenInches;
}

public static float getScreenInchSizeByXYDpi(Context context) {
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    double x = Math.pow((double)dm.widthPixels / (double)dm.xdpi, 2);
    double y = Math.pow((double)dm.heightPixels / (double)dm.ydpi, 2);
    double screenInches = Math.sqrt(x + y);

    return (float) screenInches;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top