回答:
Configuration.screenLayout
ビットマスクを使用できます。
例:
if ((getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_LARGE) {
// on a large screen device ...
}
以下のコードは、上記の答えを具体化し、画面サイズをトーストとして表示します。
//Determine screen size
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG).show();
}
else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Screen size is neither large, normal or small", Toast.LENGTH_LONG).show();
}
以下のコードは、画面密度をトーストとして表示します。
//Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density == DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else if (density == DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
静的ヘルパーメソッドとしてのJeff Gilfeltの答え:
private static String getSizeName(Context context) {
int screenLayout = context.getResources().getConfiguration().screenLayout;
screenLayout &= Configuration.SCREENLAYOUT_SIZE_MASK;
switch (screenLayout) {
case Configuration.SCREENLAYOUT_SIZE_SMALL:
return "small";
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
return "normal";
case Configuration.SCREENLAYOUT_SIZE_LARGE:
return "large";
case 4: // Configuration.SCREENLAYOUT_SIZE_XLARGE is API >= 9
return "xlarge";
default:
return "undefined";
}
}
private String getDeviceDensity() {
int density = mContext.getResources().getDisplayMetrics().densityDpi;
switch (density)
{
case DisplayMetrics.DENSITY_MEDIUM:
return "MDPI";
case DisplayMetrics.DENSITY_HIGH:
return "HDPI";
case DisplayMetrics.DENSITY_LOW:
return "LDPI";
case DisplayMetrics.DENSITY_XHIGH:
return "XHDPI";
case DisplayMetrics.DENSITY_TV:
return "TV";
case DisplayMetrics.DENSITY_XXHIGH:
return "XXHDPI";
case DisplayMetrics.DENSITY_XXXHIGH:
return "XXXHDPI";
default:
return "Unknown";
}
}
上記の回答をありがとう、それは私を大いに助けました:-)しかし、Android 1.5のサポートを余儀なくされた(私のような)人々のために、後方互換性のためにJavaリフレクションを使用できます:
Configuration conf = getResources().getConfiguration();
int screenLayout = 1; // application default behavior
try {
Field field = conf.getClass().getDeclaredField("screenLayout");
screenLayout = field.getInt(conf);
} catch (Exception e) {
// NoSuchFieldException or related stuff
}
// Configuration.SCREENLAYOUT_SIZE_MASK == 15
int screenType = screenLayout & 15;
// Configuration.SCREENLAYOUT_SIZE_SMALL == 1
// Configuration.SCREENLAYOUT_SIZE_NORMAL == 2
// Configuration.SCREENLAYOUT_SIZE_LARGE == 3
// Configuration.SCREENLAYOUT_SIZE_XLARGE == 4
if (screenType == 1) {
...
} else if (screenType == 2) {
...
} else if (screenType == 3) {
...
} else if (screenType == 4) {
...
} else { // undefined
...
}
Configuration
クラスから定数を参照できます。これらは、コンパイル時にインライン化される(つまり、実際の値に置き換えられる)静的な最終値であるため、コードが古いバージョンのプラットフォームで壊れることはありません。
targetSdkVersion
は最新のリリースに設定しました。
Androidデバイスの画面密度とサイズを簡単に知りたい場合は、この無料アプリを使用できます(権限は必要ありません)。https: //market.android.com/details?id = com.jotabout.screeninfo
xlarge画面とx..high密度を確認する必要がありますか?これは、選択した回答から変更されたコードです。
//Determine screen size
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen",Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen" , Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen" , Toast.LENGTH_LONG).show();
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(this, "XLarge sized screen" , Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Screen size is neither large, normal or small" , Toast.LENGTH_LONG).show();
}
//Determine density
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int density = metrics.densityDpi;
if (density==DisplayMetrics.DENSITY_HIGH) {
Toast.makeText(this, "DENSITY_HIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_MEDIUM) {
Toast.makeText(this, "DENSITY_MEDIUM... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_LOW) {
Toast.makeText(this, "DENSITY_LOW... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XHIGH) {
Toast.makeText(this, "DENSITY_XHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXHIGH) {
Toast.makeText(this, "DENSITY_XXHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else if (density==DisplayMetrics.DENSITY_XXXHIGH) {
Toast.makeText(this, "DENSITY_XXXHIGH... Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + String.valueOf(density), Toast.LENGTH_LONG).show();
}
これがトム・マクファーリンの答えの Xamarin.Androidバージョンです
//Determine screen size
if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeLarge) {
Toast.MakeText (this, "Large screen", ToastLength.Short).Show ();
} else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeNormal) {
Toast.MakeText (this, "Normal screen", ToastLength.Short).Show ();
} else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeSmall) {
Toast.MakeText (this, "Small screen", ToastLength.Short).Show ();
} else if ((Application.Context.Resources.Configuration.ScreenLayout & ScreenLayout.SizeMask) == ScreenLayout.SizeXlarge) {
Toast.MakeText (this, "XLarge screen", ToastLength.Short).Show ();
} else {
Toast.MakeText (this, "Screen size is neither large, normal or small", ToastLength.Short).Show ();
}
//Determine density
DisplayMetrics metrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetMetrics (metrics);
var density = metrics.DensityDpi;
if (density == DisplayMetricsDensity.High) {
Toast.MakeText (this, "DENSITY_HIGH... Density is " + density, ToastLength.Long).Show ();
} else if (density == DisplayMetricsDensity.Medium) {
Toast.MakeText (this, "DENSITY_MEDIUM... Density is " + density, ToastLength.Long).Show ();
} else if (density == DisplayMetricsDensity.Low) {
Toast.MakeText (this, "DENSITY_LOW... Density is " + density, ToastLength.Long).Show ();
} else if (density == DisplayMetricsDensity.Xhigh) {
Toast.MakeText (this, "DENSITY_XHIGH... Density is " + density, ToastLength.Long).Show ();
} else if (density == DisplayMetricsDensity.Xxhigh) {
Toast.MakeText (this, "DENSITY_XXHIGH... Density is " + density, ToastLength.Long).Show ();
} else if (density == DisplayMetricsDensity.Xxxhigh) {
Toast.MakeText (this, "DENSITY_XXXHIGH... Density is " + density, ToastLength.Long).Show ();
} else {
Toast.MakeText (this, "Density is neither HIGH, MEDIUM OR LOW. Density is " + density, ToastLength.Long).Show ();
}
この関数を試してくださいisLayoutSizeAtLeast(int screenSize)
小さな画面を確認するには、少なくとも320x426 dp以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_SMALL);
通常の画面を確認するには、少なくとも320x470 dp以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_NORMAL);
大画面を確認するには、少なくとも480x640 dp以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE);
特大画面を確認するには、少なくとも720x960 dp以上 getResources()。getConfiguration()。isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_XLARGE);
2018年にKotlinでJeffの回答が必要な場合は、次のとおりです。
private fun determineScreenSize(): String {
// Thanks to https://stackoverflow.com/a/5016350/2563009.
val screenLayout = resources.configuration.screenLayout
return when {
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_SMALL -> "Small"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_NORMAL -> "Normal"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_LARGE -> "Large"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_XLARGE -> "Xlarge"
screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK == Configuration.SCREENLAYOUT_SIZE_UNDEFINED -> "Undefined"
else -> error("Unknown screenLayout: $screenLayout")
}
}
文字列リソースと列挙型を使用してこれを行うことはできませんか?SMALL、MEDIUM、LARGEなどの画面サイズの名前を持つ文字列リソースを定義できます。次に、文字列リソースの値を使用して、列挙型のインスタンスを作成できます。
気になるさまざまな画面サイズのEnumをコードで定義します。
public Enum ScreenSize {
SMALL,
MEDIUM,
LARGE,;
}
文字列リソース、たとえばscreensizeを定義します。その値はSMALL、MEDIUM、またはLARGEのいずれかです。
<string name="screensize">MEDIUM</string>
screensize
気になる各次元の文字列リソースにのコピーを置きます。<string name="screensize">MEDIUM</string>
、values-sw600dp / strings.xmlおよびvalues-medium / strings.xmlおよび<string name="screensize">LARGE</string>
移動し、sw720dp / strings.xmlおよびvalues-large / strings.xmlに移動します。ScreenSize size = ScreenSize.valueOf(getReources().getString(R.string.screensize);
このコードをコピーしてに貼り付け、Activity
実行するとToast
、デバイスの画面サイズカテゴリが表示されます。
int screenSize = getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK;
String toastMsg;
switch(screenSize) {
case Configuration.SCREENLAYOUT_SIZE_LARGE:
toastMsg = "Large screen";
break;
case Configuration.SCREENLAYOUT_SIZE_NORMAL:
toastMsg = "Normal screen";
break;
case Configuration.SCREENLAYOUT_SIZE_SMALL:
toastMsg = "Small screen";
break;
default:
toastMsg = "Screen size is neither large, normal or small";
}
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();