Android 基于设备大小的布局

Android 基于设备大小的布局,android,android-layout,android-emulator,android-screen-support,android-screen,Android,Android Layout,Android Emulator,Android Screen Support,Android Screen,我现在很困惑。我试着制作不同的图像,并将它们放在适当的文件夹(xhdpi,ldpi)中,但我的应用程序不能适用于2.7英寸到10英寸的每种设备类型。所以我决定为不同的屏幕尺寸做不同的布局。我从该代码中获取设备大小(以英寸为单位)—— DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); double x = Math.pow(dm.widthPixels/d

我现在很困惑。我试着制作不同的图像,并将它们放在适当的文件夹(xhdpi,ldpi)中,但我的应用程序不能适用于2.7英寸到10英寸的每种设备类型。所以我决定为不同的屏幕尺寸做不同的布局。我从该代码中获取设备大小(以英寸为单位)——

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
double x = Math.pow(dm.widthPixels/dm.xdpi,2);
double y = Math.pow(dm.heightPixels/dm.ydpi,2);
double screenInches = Math.sqrt(x+y);
Log.d("debug","Screen inches : " + screenInches);

现在我想知道的是,如果设备大小大约为2.7“我的应用程序开始使用自定义文件夹中的布局。这可能吗?

您可以在布局文件中的一个小部件上包含带有不同限定符的标记,并在运行时检查这些标记

layout-large/layout1.xml:
     <Button
    android:id="@+id/btn1"
    ...
    android:tag="large" />

layout-small/layout1.xml:
     <Button
    android:id="@+id/btn1"
    ...
    android:tag="small" />

请看我的答案创建不同的布局不是好的做法。只使用一个布局,并确保您的小部件在没有任何硬编码值的情况下定位/对齐。
findViewById(R.id.btn1).getTag().toString()