如何让我的应用在所有设备上看起来都一样?

How to make my app look the same in all devices?

我刚开始 android 开发,我在 android studio 上构建了一个应用程序,我想让我的应用程序在所有设备上都具有相同的设计:

Samsung Galaxy S4 => 1080x1290 ; 5.0”

银河连结 => 720x1280 ; 4.7”

连结 4 => 768x1280 ; 4.7”

摩托罗拉 Droid Razr M => 540x960 ; 4.3”

Nexus S => 480x800 ; 4”

银河 S2 => 480x800 ; 4.3”

银河王牌 => 320x480 ; 3.5”

银河笔记 => 800x1280 ; 5.3”

银河笔记 II => 720x1280 ; 5.5”

连结 10 => 2560 x 1600 ; 10.1”

银河标签 10.1 => 1280*800 ; 10.1”

银河笔记 8.0 => 1280*800 ; 8.0”

银河标签 7.7 => 1280*800 ; 7.7”

连结 7 => 1280*800 ; 7.0”

银河标签 => 1024*600 ; 7.0”


我在这里阅读了这些以及很多问题

http://developer.android.com/training/multiscreen/index.html

http://developer.android.com/guide/practices/screens_support.html#DeclaringTabletLayouts

我在 "dp" 中使用了 width/height 并用所有项目制作了这个

android:layout_margin
android:paddingLeft
android:paddingRight
android:paddingTop
android:paddingBottom

它工作正常,但据我所知,有一种方法可以读取设备屏幕然后决定在应用程序中打开哪个布局,假设我有一个布局并且我有这些尺寸

res/layout/main_activity.xml # For handsets (smaller than 600dp available width) res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger) res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)


我的问题是:我如何编写 java activity 代码来读取我的应用程序 运行 所在的设备屏幕,然后决定打开哪个布局如果设备是 S2,那么 java 会做这样的事情:

if (tabletDeviceSize) {

//use tablet support layout

} else

{

//another layout for mobile support

}

我看到了这段代码,但我需要完整的代码,因为我的编码并不完美:(

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
switch(metrics.densityDpi){
  case DisplayMetrics.DENSITY_LOW:
            break;
  case DisplayMetrics.DENSITY_MEDIUM:
             break;
  case DisplayMetrics.DENSITY_HIGH:
             break;
}

如果您问的是如何获得设备屏幕的密度,我花了 3 秒才找到答案(不包括在我最喜欢的搜索引擎中输入 "android get device density" 的时间):

getting the screen density programmatically in android?

根据 official documentation,您无需以编程方式决定针对相应屏幕尺寸使用哪种布局。

To optimize your application's UI for the different screen sizes and densities, you can provide alternative resources for any of the generalized sizes and densities. Typically, you should provide alternative layouts for some of the different screen sizes and alternative bitmap images for different screen densities. At runtime, the system uses the appropriate resources for your application, based on the generalized size or density of the current device screen.

换句话说,如果您遵循文档中的建议,正如我所看到的那样,将您的布局文件放在它们各自的资源文件夹中,如下所示:

res/layout/main_activity.xml # For handsets (smaller than 600dp available width)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger) 
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)

然后系统将决定使用哪种布局。您无需额外代码即可在 运行 时指定它。

但是如果您想根据屏幕 分辨率 进行更改,您可以使用以下代码

获取以像素为单位的宽度和高度
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

然后根据 widthheight 变量做一些切肉刀,例如在您使用 S2 的情况下:

if(width == 480 && height == 800){
    //Do work that's related to the S2 screen resolution
}else if(...){

}