मिन एपीआई 13 और इसके बाद के संस्करण @Tobliug जवाब- सबसे अच्छा समाधान के साथ जाना।
Configuration config = getResources().getConfiguration();
config.smallestScreenWidthDp;// But it requires API level 13
एपीआई स्तर 13 नीचे इस सवाल का जवाब कोशिश
SmallWidthCalculator.java बनाएं वर्ग & सिर्फ इस कोड
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
public class SmallWidthCalculator {
private static SmallWidthCalculator ourInstance = new SmallWidthCalculator();
public static SmallWidthCalculator getInstance() {
return ourInstance;
}
private Context mContext;
private SmallWidthCalculator() {
}
public double getSmallWidth(Context context) {
mContext = context;
DisplayMetrics dm = context.getResources().getDisplayMetrics();
int width = dm.widthPixels;
int height = dm.heightPixels;
double dpi = getDPI(width, height);
double smallWidthDPI = 0;
int smallWidth = 0;
if (width < height)
smallWidth = width;
else
smallWidth = height;
smallWidthDPI = smallWidth/(dpi/160);
return smallWidthDPI;
}
private double getDPI(int width,
int height) {
double dpi = 0f;
double inches = getScreenSizeInInches(width, height);
dpi = Math.sqrt(Math.pow(width, 2) + Math.pow(height, 2))/inches;
return dpi;
}
private double getScreenSizeInInches(int width, int height) {
if (mContext != null) {
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
double wi = (double) width/(double) dm.xdpi;
double hi = (double) height/(double) dm.ydpi;
double x = Math.pow(wi, 2);
double y = Math.pow(hi, 2);
return Math.sqrt(x + y);
}
return 0;
}
}
अपनी गतिविधि या से पेस्ट कॉपी टुकड़ा सिर्फ अपने संदर्भ से पारित और अपने छोटी चौड़ाई
double smallWidthDp=SmallWidthCalculator.getInstance().getSmallWidth(this);
स्रोत
2017-02-26 17:15:50
अक्सर आप जब तक आप उचित संसाधन फ़ाइलों प्रदान पता करने की जरूरत नहीं है। [यह उत्तर] देखें (http://stackoverflow.com/a/41646301/3681880)। – Suragch