简体   繁体   English

支持9.6英寸和10.1英寸android平板电脑

[英]Supporting 9.6 inch and 10.1 inch android tablet

I have to support android devices with screen resolution 1280*800(in dp)(KitKat) and 1280 * 752(dp)(Lollipop). 我必须支持屏幕分辨率为1280 * 800(in dp)(KitKat)和1280 * 752(dp)(Lollipop)的android设备。 The first one is a 10.1 inch tablet and second one is 9.6 inch tablet. 第一个是10.1英寸的平板电脑,第二个是9.6英寸的平板电脑。

I am using same layout for all device and using externalized dimension to adjust layout for different screen. 我为所有设备使用相同的布局,并使用外部尺寸调整不同屏幕的布局。

But I am not able to separate the device using "values-sw720dp" and "values-sw800dp". 但是我无法使用“ values-sw720dp”和“ values-sw800dp”将设备分开。 If I use sw800dp both of them use the dimen value from sw800dp dimen folder. 如果我使用sw800dp,则它们都使用sw800dp dimen文件夹中的dimen值。

How can I give separate dimension for the two devices? 如何为两个设备分别指定尺寸?

Get your device's screnn inches programmatically and appy dimension 通过编程获得设备的屏幕英寸和尺寸

public static double getDeviceInches(Activity activity) {
    DisplayMetrics metrics = getMetrics(activity);
    int widthPixels = metrics.widthPixels;
    int heightPixels = metrics.heightPixels;

    float widthDpi = metrics.xdpi;
    float heightDpi = metrics.ydpi;

    float widthInches = widthPixels / widthDpi;
    float heightInches = heightPixels / heightDpi;

    return getDiagonalInches(widthInches, heightInches);
}

private static double getDiagonalInches(float widthInches, float heightInches) {
     double diagonalInches = Math.sqrt((widthInches * widthInches) + (heightInches * heightInches));
     float roundedValue = (float) Math.round(diagonalInches);
     return (double)roundedValue;
}

//From this, we can get the information required to size the display:
private static DisplayMetrics getMetrics(Activity activity) {
    DisplayMetrics metrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
    return metrics;
}

public static int convertDpToPx(Context context, int value) {
    // Get the screen's density scale
    final float scale = context.getResources().getDisplayMetrics().density;
    // Convert the dps to pixels, based on density scale(0.5f is for rounding up value) (arrowWidth is 50dp)
    int pxValue= (int) (value * scale + 0.5f);
    return pxValue;
}

Put your above code in utility class and call getDeviceInches method from your acitivty or fragment class 将上面的代码放入实用程序类,并从您的活动或片段类中调用getDeviceInches方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM