简体   繁体   English

如何知道我是否应该在布局中使用ldpi

[英]How to know if I should use ldpi in a layout

Hi guys, I'm working on implementing multiple screen support. 大家好,我正在努力实现多屏支持。 Is a similar case possibile? 是否有类似的情况?

 @Override
 protected void onCreate(Bundle savedInstanceState) {

     super.onCreate(savedInstanceState);
     setContentView(R.layout.contatti);

    //if (device in use , use ldpi ) { do something
    else{


     Button btnNavigator = (Button) findViewById(R.id.btnNavigator);

     GoogleMap map=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(STARTING_POINT, 5));

Thank you so much. 非常感谢。 Can you show me an example please? 你能给我举个例子吗?

Try this code: 试试这个代码:

private final boolean isLdpi()
{
    final DisplayMetrics metrics =
        Resources.getSystem().getDisplayMetrics();

    final float scale = metrics.density;

    return (scale == 0.75); // ldpi = 0.75, mdpi = 1.0, hdpi = 1.5, xhdpi = 2.0, xxhdpi = 3.0, ...
}

Usage: 用法:

if (isLdpi)
{
    // It's ldpi: do something
}

According to google 根据谷歌

small screens are at least 426dp x 320dp. 小屏幕的尺寸至少为426dp x 320dp。

So find out the width of the device and then compare. 因此,找出设备的宽度,然后进行比较。

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//this gives you the pixels and no the dp
metrics.heightPixels;
metrics.widthPixels;

//convert to dp
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * (metrics.densityDpi / 160f);
return px;
}

Please see here for more details. 在此处查看更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM