【问题标题】:Supporting 9.6 inch and 10.1 inch android tablet支持9.6寸和10.1寸安卓平板
【发布时间】:2016-12-26 06:36:24
【问题描述】:

我必须支持屏幕分辨率为 1280*800(in dp)(KitKat) 和 1280 * 752(dp)(Lollipop) 的 Android 设备。第一个是 10.1 英寸的平板电脑,第二个是 9.6 英寸的平板电脑。

我对所有设备使用相同的布局,并使用外部尺寸来调整不同屏幕的布局。

但我无法使用“values-sw720dp”和“values-sw800dp”分离设备。如果我使用 sw800dp,它们都使用 sw800dp dimen 文件夹中的 dimen 值。

我怎样才能为两个设备提供单独的尺寸?

【问题讨论】:

标签: android


【解决方案1】:

以编程方式获取设备的屏幕英寸和应用尺寸

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;
}

将上面的代码放在实用程序类中,并从您的活动或片段类中调用 getDeviceInches 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多