Ich benötige dies für einige meiner Apps und der folgende Code war meine Lösung für das Problem. Nur den Code in onCreate anzeigen. Dies ist eine eigenständige App, die auf jedem Gerät ausgeführt werden kann, um die Bildschirminformationen zurückzugeben.
setContentView(R.layout.activity_main);
txSize = (TextView) findViewById(R.id.tvSize);
density = (TextView) findViewById(R.id.density);
densityDpi = (TextView) findViewById(R.id.densityDpi);
widthPixels = (TextView) findViewById(R.id.widthPixels);
xdpi = (TextView) findViewById(R.id.xdpi);
ydpi = (TextView) findViewById(R.id.ydpi);
Configuration config = getResources().getConfiguration();
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
Toast.makeText(this, "Large screen", Toast.LENGTH_LONG).show();
txSize.setText("Large screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_NORMAL) {
Toast.makeText(this, "Normal sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Normal sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {
Toast.makeText(this, "Small sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
Toast.makeText(this, "xLarge sized screen", Toast.LENGTH_LONG)
.show();
txSize.setText("Small sized screen");
} else {
Toast.makeText(this,
"Screen size is neither large, normal or small",
Toast.LENGTH_LONG).show();
txSize.setText("Screen size is neither large, normal or small");
}
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
Log.i(TAG, "density :" + metrics.density);
density.setText("density :" + metrics.density);
Log.i(TAG, "D density :" + metrics.densityDpi);
densityDpi.setText("densityDpi :" + metrics.densityDpi);
Log.i(TAG, "width pix :" + metrics.widthPixels);
widthPixels.setText("widthPixels :" + metrics.widthPixels);
Log.i(TAG, "xdpi :" + metrics.xdpi);
xdpi.setText("xdpi :" + metrics.xdpi);
Log.i(TAG, "ydpi :" + metrics.ydpi);
ydpi.setText("ydpi :" + metrics.ydpi);
Und eine einfache XML-Datei
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/density"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/densityDpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/widthPixels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/xdpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/ydpi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>