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

时间:2014-05-17 18:03:04

标签: android android-layout

大家好,我正在努力实现多屏幕支持。 类似的情况是否可能?

 @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));

非常感谢你。你能告诉我一个例子吗?

2 个答案:

答案 0 :(得分:2)

试试这段代码:

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, ...
}

用法:

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

答案 1 :(得分:0)

根据谷歌

  

小屏幕至少为426dp x 320dp。

因此找出设备的宽度然后进行比较。

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;
}

有关详细信息,请参阅here

相关问题