2014-10-23 7 views
10

मैं 4 विभिन्न उपकरणों है:एंड्रॉइड डिवाइस की सबसे छोटी चौड़ाई (एसए) कैसे जानी जाए?

  1. Asus गोली, screensize 7 "
  2. लेनोवो गोली, screensize 7"
  3. एचटीसी मोबाइल फोन, screensize 5 "
  4. एचटीसी मोबाइल फोन, screensize 4.7"

मैं इसके लिए एक समर्थन लेआउट बनाने के लिए अपने डिवाइस की सबसे छोटी चौड़ाई (sw) जानना चाहता हूं।

मैं "लेआउट-sw600dp" जैसे संसाधन फ़ोल्डर बनाना चाहता हूं लेकिन मुझे छोटी चौड़ाई (sw) का मान नहीं पता है।

मैं sw इस कोड का उपयोग प्रिंट करने की कोशिश:

DisplayMetrics metrics = getResources().getDisplayMetrics(); 
Log.i("Density", ""+metrics.densityDpi); 

लेकिन अगर यह सही मान है मुझे नहीं पता।

मुझे सबसे छोटी चौड़ाई (sw) कैसे मिलती है?

+0

अक्सर आप जब तक आप उचित संसाधन फ़ाइलों प्रदान पता करने की जरूरत नहीं है। [यह उत्तर] देखें (http://stackoverflow.com/a/41646301/3681880)। – Suragch

उत्तर

8

आप इस कोशिश कर सकते हैं:

DisplayMetrics dm = mActivity.getApplicationContext() 
        .getResources().getDisplayMetrics(); 
      float screenWidth = dm.widthPixels/dm.xdpi; 
      float screenHeight = dm.heightPixels/dm.ydpi; 

विवरण के लिए:

DisplayMetrics dm = getApplicationContext().getResources().getDisplayMetrics(); 
float screenWidth = dm.widthPixels/dm.density; 
float screenHeight = dm.heightPixels/dm.density; 

जो भी: उपरोक्त जवाब देने के लिए here

+0

हाँ आप रनटाइम में पता लगा सकते हैं लेकिन आपको संकलन से पहले अपना फ़ोल्डर बनाना था। तो जब आप mdpi, hdpi और ... के बजाय sw का उपयोग करना चाहते हैं तो sw के लिए बाल्टी क्या है? – Kenji

+0

यह एसडब्ल्यू वापस नहीं करेगा। यह 'screenWidth = dm.widthPixels /dm.density होना चाहिए;' – VSB

8

सुधार, सही कोड सही sw इस कोशिश को खोजने के लिए स्क्रीन की चौड़ाई और स्क्रीन हाइट छोटा है आपका sw।

+0

ऐसा लगता है कि यह सही उत्तर है। स्वीकार्य उत्तर मुझे नोट 5 पर 2.79 देता है। नोट 5 मेरे sw360dp फ़ोल्डर से मूल्यों को पकड़ रहा है, इसलिए यह छोटी चौड़ाई निर्धारित करने का सटीक तरीका नहीं होना चाहिए। इच्छा है कि Google ने मुझे आधिकारिक डॉक्टर पर फॉर्मूला केवल मुझे कंघी स्टैक ओवरफ्लो बनाने के बजाय आश्वासन के लिए फॉर्मूला किया था ... – welshk91

+0

हमें 160 से पीएक्स * 160 की तरह गुणा करने की आवश्यकता नहीं है)/डीपीआई –

3

मिन एपीआई 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); 
7

यहां एक और आसान तरीका है।

आप डेवलपर विकल्प से जांच कर सकते हैं छोटी से छोटी चौड़ाई डिवाइस की (एंड्रॉयड स्टूडियो एम्यूलेटर & में यह मेरी रियल डिवाइस में नहीं मिला है)।

enter image description here

+0

यह एंड्रॉइड 7+ पर काम करता है। – asym

संबंधित मुद्दे